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

Popular posts from this blog

1548288703

Mon_Apr_15_04:01:22_PDT_2019