Fri_Feb__8_07:01:27_PST_2019
Swift - Tuples Advertisements Previous Page Next Page ï¿ï¾ Swift 4 also introduces Tuples type, which are used to group multiple values in a single compound Value. The values in a tuple can be of any type, and do not need to be of same type. For example, ("Tutorials Point", 123) is a tuple with two values, one of string Type, and other is integer type. It is a legal command. let ImplementationError = (501, "Not implemented") is an error when something on the server is not implemented, It returns two values. Error Code, and Description. You can create tuples from as many values as you want and from any number of different data types. Hereï¿¢ï¾ï¾s the syntax of Tuple declaration ï¿¢ï¾ï¾' var TupleName = (Value1, value2,ï¿¢ï¾ï¾¦ any number of values) Hereï¿¢ï¾ï¾s a Tuple declaration ï¿¢ï¾ï¾' var error501 = (501, ï¿¢ï¾ï¾Not implementedï¿¢ï¾ï¾) You can access the values of tuple using the index numbers that start from 0. Hereï¿¢ï¾ï¾s an example of accessing tuple Values ï¿¢ï¾ï¾' print(ï¿¢ï¾ï¾The code is\(error501.0)ï¿¢ï¾ï¾) print(ï¿¢ï¾ï¾The definition of error is\(error501.1)ï¿¢ï¾ï¾) You can name the variables of a tuple while declaring , and you can call them using their names var error501 = (errorCode: 501, description: ï¿¢ï¾ï¾Not Implementedï¿¢ï¾ï¾) print(error501.errorCode) // prints 501. Tuples are helpful in returning multiple values from a function. Like, a web application might return a tuple of type ("String", Int) to show whether the loading was successful or failed. By returning different values in a tuple we can make decisions depending on different tuple types. Note ï¿¢ï¾ï¾' Tuples are useful for temporary values and are not suited for complex data. Previous Page Print Next Page ï¿ï¾ Advertisements
Comments
Post a Comment