Richy Garrincha
Richy Garrincha

Reputation: 277

Returning a tuple in Swift

sorry for such a basic question but I ve only just started working with tuples

this is my code

func test() -> (authorName:String, numberOfViews:Int) {

    let author : String = ""
    let numberViews = 0


    return(authorName : author, numberOfView : numberViews)


}

can anyone provide the correct way to do this

thanks in advance

Upvotes: 7

Views: 5173

Answers (2)

Alejandro
Alejandro

Reputation: 220

For create a tuple simply put it in normal brackets and separate each other with comas, you also can do it on te return function Example :
let exampleTuple = (23, "A string", 5.583)

The article from Apple :

Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.In this example, (404, "Not Found") is a tuple that describes an HTTP status code. An HTTP status code is a special value returned by a web server whenever you request a web page. A status code of 404 Not Found is returned if you request a webpage that doesn’t exist.

let http404Error = (404, "Not Found")

Upvotes: 0

gutte
gutte

Reputation: 1473

according to the Apple's swift book:

func test() -> (authorName:String, numberOfViews:Int) {

let author : String = ""
let numberViews = 0


return(author, numberViews)
}

you define the return object at the declaration. and in the return statement just put the values.

Upvotes: 15

Related Questions