Home Swift return type clarification
Reply: 4

Swift return type clarification

Deepak Sharma
Deepak Sharma Published in 2018-02-08 11:49:46Z

I see a Swift function written as follows:

func calculation(imageRef: CGImage) -> (red: [UInt], green: [UInt], blue: [UInt]) {


I need to know what is the return type in the function above. I am unable to relate it to any of the known types & also what is its equivalent in Objective-C if any at all? What's the way to translate this function to Objective-C?

the4kman Reply to 2018-02-08 11:52:10Z

It's a tuple that this function is returning.

A tuple can hold various types in one object, but unlike an array, you cannot append or remove objects to/from it.

From Apple Developer Swift Guides:

Tuples group multiple values into a single compound value. The values within a tuple can be of any type and don’t have to be of the same type as each other.

Tuples don't exist in Objective-C. You can find more information about that here.

Prashant Tukadiya
Prashant Tukadiya Reply to 2018-02-08 11:51:59Z

This called Tuple learn here

it allows to group multiple values under single variable.

Objective c don't support tuple . in objc you have to use dictionary and you have to use key red , green and 'blue with array as value

Badhan Ganesh
Badhan Ganesh Reply to 2018-02-08 13:10:25Z
func calculation(imageRef: CGImage) -> (red: [UInt], green: [UInt], blue: [UInt]) {


The above method returns tuple (A group of different values that you can use in Swift).

You can also return tuple without named parameters:

func calculation(imageRef: CGImage) -> ([UInt], [UInt],[UInt]) {


You can access the return values like this (For un-named tuple parameters):

let returnedTuple = calculation(imagRef)
print(returnedTuple.0) //Red
print(returnedTuple.1) //Green
print(returnedTuple.2) //Blue

or (For named tuple parameters):

let returnedTuple = calculation(imagRef)
print(returnedTuple.red) //Red
print(returnedTuple.green) //Green
print(returnedTuple.blue) //Blue

There is no equivalence of tuple in Objective-C.

Sh_Khan Reply to 2018-02-08 11:51:25Z

This method return a tuple

func calculation(imageRef: CGImage) -> (red: [UInt], green: [UInt], blue: [UInt]) {

    return ([],[],[])
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.40033 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO