Return a tuple in Swift

sorry for such a basic question, but I 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 someone provide the correct way to do this

early

+5
source share
2 answers

according to Apple's operational book:

 func test() -> (authorName:String, numberOfViews:Int) { let author : String = "" let numberViews = 0 return(author, numberViews) } 

You define the return object in the declaration. and in the return expression just put the values.

+8
source

To create a tuple, just put it in normal brackets and separate it with a coma, you can also do this by the return function Example:
let exampleTuple = (23, "A string", 5.583)

Apple article:

Tuples group multiple values ​​into one composite value. The values ​​inside the tuple can be of any type and should not be of one type, like the others. In this example (404, "Not Found") is a tuple that describes the HTTP status code. The HTTP status code is a special value returned by the web server whenever you request a web page. The 404 Not Found status code is returned if you request a web page that does not exist.

let http404Error = (404, "Not Found")

0
source

Source: https://habr.com/ru/post/1206578/


All Articles