Swift return type clarification

后端 未结 4 1890
既然无缘
既然无缘 2021-01-29 13:05

I see a Swift function written as follows:

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

 ...
 ...
}

4条回答
  •  梦如初夏
    2021-01-29 13:29

    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.

提交回复
热议问题