Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Tuples in Swift

    • 0
    • 3
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 630
    Comment on it

    Tuples is a new concept that is introduced in swift programing. Tuples group the multiple values in a single compound value. It is not necessary that the each tuples value should have the same datatype. The datatype can be different and number of values can be added to the tuples.

    let error=(0000,"Not Found");
    

    Tuple groups together an Int and a String . Error is a collection of a number and a human readable string. It is described as a tuple of type (Int, String).

    You can create tuples from any combination of datatype like (Int,Int,Bool) or (Bool,Int).

    You can decompose a tuples content into separate constants or variables, which you then access as usual:

    let (errorCode,errorMessage)=error
             println("value of the status\(errorCode) and message\(errorMessage)")
    

    You can decompose the tuples values according to need if you want only to access the only errorCode or errorMessage then you can do like this

    let (onlyErrorCode, _) = error
        println("The error code is \(onlyErrorCode)")
        // prints "The error code is 0000
    
    and rest of the values ignored by an underscore sign(_)

    Alternatively, access the individual element values in a tuple using index numbers starting at zero:

    println("The Error code is \(error.0)")
        // prints "The Error code is 0000
        println("The Error message is \(error.1))
        // prints "The Error message is Not Found"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: