Swift Learning-基本内容

2970 ワード

Head


本文は基本的にswift翻訳から勉強して、メモを取りましょう.

Content

  • 定数変数付与
  •         // 1  
            var helloMeetyou = "hello Meetyou !";
            print(helloMeetyou);
            helloMeetyou = helloMeetyou + " hahahha append";
            print(helloMeetyou);
            let cat = "";
            print(cat);
    
  • タイプ別名
  • typealias myString = String;
    
            // 2  
            let mystring = myString(111);
            if mystring == "111" {
                print(mystring);
            }
    

    3.タプル
            // 3  
            let (successCode,successDesc) = (200,"successs");
            print("successCode is \(successCode)");
            print("successDesc is \(successDesc)");
    

    4.オプション C Objective-C , 。 Objective-C , nil 。 nil “ ”。 , , , C 。 ,Objective-C ( NSNotFound ) 。 。 ,Swift , 。
            // 4  
    //        let isNil : Int? = Int("hhahah");
            let isNil : Int? = Int("404");
            if isNil != nil {
                print("isNil is a number \(isNil!)!");
            }
            //   (!) 
            if let actulyValue = isNil {
                print("actulyValue is \(actulyValue)");
            } else {
                print("isNil is empty");
            }
    

    5.暗黙的オプション&オプション比較 。 ( String! ) ( String? ) 。
            // 5   &    
            // 5.1 
            let possibleString : String? = "a possible String";
            if possibleString != nil {
                print(possibleString!);
            }
            if let actuly = possibleString {
                print(actuly);
            }
            // 5.2 
            let actulyString : String! = possibleString;
            if actulyString != nil {
                print(actulyString!);
            }
            if let actylyStrubg2 = actulyString {
                print(actylyStrubg2)
            }
    
  • do try catch , , , try? java .....
  •         // 6 do try catch
            do {
                try tryFunc();
                continueFunc();
            } catch {
                errorFunc();
            }
    
        // 6 do try catch
        func tryFunc() throws {
    //        let optionalInt : Int? = Int("11hhh");
    //        print(optionalInt!);
    //        let arr = ["1","2"];
    //        print(arr[4]);
            print("tryFunc");
            throw tryError.tryError1;
        }
        func continueFunc() {
            print("continueFunc");
        }
        func errorFunc() {
            print("Error :(");
        }
    
    enum tryError : Error {
        case tryError1
    }
    
  • 断言&前処理
  •         // 7  
            let dy = 1;
            assert(dy >= 0,"dy >= 0");
    //
            // 8  
            precondition(dy >= 0,"dy > 0");
    

    End


    ますます怠惰になってきた........(