Swift-タプルとオプションタイプ


一.タプル(Tuples)
タプルは、関数の戻り値などの一時的な関連データに適しており、複雑なデータ構造には適していません.複雑なデータ構造はクラスまたは構造体で定義されています.
//1.      

//       ,          

let http404Error = (404, "Not Found")

//2.         

let (statusCode, statusMessage) = http404Error

print("The status code is \(statusCode)") //The status code is 404

print("The status message is \(statusMessage)") //The status message is Not Found

//          ,     (_)  

let (justTheStausCode, _) = http404Error

print("The status code is \(justTheStausCode)")

//   

print("The status code is \(http404Error.0)")

print("The status message is \(http404Error.1)")

//3.               

let http200Status = (statusNum: 200, description: "OK")

print("The status code is \(http200Status.statusNum)")

print("The status code is \(http200Status.description)")
// .     (Optionals)
        //    :     + ?
        //          ,     
        
        //1.           
        //              ,          
        let possibleNumber = "123"//                 Int  ,   nil
        let str = "gagag"
        
        let convertedNumber = Int(possibleNumber)//convertedNumber        Int?(    ),         Int ,     
        print(convertedNumber, Int(str))
        //  : Optional(123), nil
        print(convertedNumber!)//     
        //  : 123
        
        //2.       nil
        /*
           OC   nil  ,OC   nil     ,        。
          Swift  nil    ,        ,             nil,     
         
                   nil,                nil
         */
        var serverResponseCode: Int? = 400
        serverResponseCode = nil
        //            ,    nil
        var surveyAnswer: String? //surveyAnswer = nil
        
        //3. if  
        //   if              
        if convertedNumber != nil {
            print("convertedNumber contains some integer value.")
        }
        
        //4.     (Forced Unwrapping)
        //           (   nil) ,         !,    
        //             ,       .
        if convertedNumber != nil {
            print("\(convertedNumber!)")
        }
        //      nil,        run-time error.                     nil
        let testNum: Int? = nil
//        print(testNum!)
        
        //5.     (Optional Binding)
        //                  ,          ,     nil ,   if  ,while    
        //                     
        //guard                         
        
        if let actualNumber = Int(possibleNumber) {
            print("\"\(possibleNumber)\" has an integer value of \(actualNumber)")
        }else {
            print("\"\(possibleNumber)\" could not be converted to an integer")
        }
        
        if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 {
            print("\(firstNumber) < \(secondNumber) < 100")
        }//       
        
        if let firstNum = Int("4") {
            if let secondNum = Int("42") {
                if firstNum < secondNum && secondNum < 100 {
                    print("\(firstNum) < \(secondNum) < 100")
                }
            }
        }
        
        //6.         (Implicitly Unwrapped Optionals)
        //    :     + !
        //         
        //      (   )           ,              
        //            ,           。             nil ,        !
        //                ,                           ,       !
    
        let assumedString: String! = "An implicitly unwrapped optional string."
        let implicitString = assumedString
        //      nil,        run-time error.                     nil
//        var nilValue: String?; print(nilValue!)
        //      nil,                  compile-time error
        var nilValue: Int!; print(nilValue)
        
        //            ,    if        ,                  
        if assumedString != nil {
            print(assumedString)
        }
        if let definiteString = assumedString {
            print(definiteString)
        }
       
        
        // .     (Error Handling)
        //                 nil,             。       ,                 ,                    
        //        throws    ,       。         try    。
        //Swift              catch    
        
        func makeASandwich() throws {
            //
        }
        
        /*
         do {
             //     
             try makeASandwich()
             eatASandwich()
         } catch SandwichError.outOfCleanDishes {
             //      
             washDishes()
         } catch SandwichError.missingIngredients(let ingredients) {
             //     
             buyGroceries(ingredients)
         }
         */
        
        // .        (Assertions and Preconditions)
        //      (Debug Builds),        (Debug Builds) (Production Builds)
        
        let age = -3
//        assert(age >= 0, "A person's age can't be less than zero.")
        
        if age > 10 {
            print("You can ride the roller-coaster or the ferris wheel.")
        } else if age > 0 {
            print("You can ride the ferris wheel.")
        } else {
            //  
//            assertionFailure("A person's age can't be less than zero.")
        }
        
        //    ,            ,             
        // fatalError(_:file:line:)
        //    view     
//        required init?(coder aDecoder: NSCoder) {
//            fatalError("init(coder:) has not been implemented")
//        }