Swift Learning-演算子&文字列

3799 ワード

Head
本文は基本的にswift翻訳から勉強して、メモを取りましょう.
Content
  • ユニット比較
  •         // 1.    
            //         
            //               ,     ,             。           ,             
            let x = (3,"c");
            let y = (3,"bfffsdfsdfsdfsdsdfsdfsdasdfasfffffdvcxcxvc");
            if x > y {
                print("x > y");
            } else if x == y {
                print("x == y");
            } else if x < y{
                print("x < y");
            }
    
  • 連結Null演算子
  •         let a : Int? =  Int("123a");
            print(a ?? 2); // a != nil ? a! : 2
    

    3.区間
            // 3.1    1...5
            for index in 1...5 {
                print(index,terminator: ",");
            }
            print("");
            // 3.2    
            for index in 1..<5 {
                print(index,terminator:",");
            }
            print("");
            // 3.3     
            let names = ["Anna", "Alex", "Brian", "Jack"];
            for index in names[1...2] {
                print(index,terminator:",");
            }
            print("");
            for index in names[0..

    4.文字列
           // 4.1      - """
            let quotation = """
            The White Rabbit put on his spectacles.  "Where shall I begin,
            please your Majesty?" he asked.
            "Begin at the beginning," the King said gravely, "and go on
            till you come to the end; then stop."
            """
            print(quotation);
    

    改行する
            // 4.3  \     
            let softWrappedQuotation = """
            The White Rabbit put on his spectacles.  "Where shall I begin, \
            please your Majesty?" he asked.
            "Begin at the beginning," the King said gravely, "and go on \
            till you come to the end; then stop."
            """
            print(softWrappedQuotation);
    
  • 特殊文字 \0 ( ), \\ ( ), \t ( ),
    ( ), \r( ), \" ( ) \' ( ); Unicode , \u{n}, n 1-8 Unicode 16 。
  • let spec = ["
    ","\0","\\","\t","\"","\r","\'","\u{1F496}"]; for item in spec { print(item); } print("");
  • 拡張文字区切り記号
  •       print(#"hahahahha\#nhhheehhehehe"#);
            
            let string1 = "aaaaaa";
            let string2 = string1;
            print(Unmanaged.passUnretained(string1 as AnyObject).toOpaque());
            print(Unmanaged.passUnretained(string2 as AnyObject).toOpaque());
            
            
            let characterArr : [Character] = ["1","2","!","
    ",""]; let charaString : String? = String(characterArr); print(charaString ?? ""); let multiplier = 3; print("\(multiplier) times 2.5 is \(Double(multiplier) * 2.5) ")
  • 挿入&切り取り文字列
  •       //      
            var welcome = "hello";
            welcome.insert("!", at: welcome.endIndex);
            print(welcome);
            welcome.insert(contentsOf : " world", at: welcome.index(before: welcome.endIndex));
            print(welcome);
    //
            //      
            welcome = welcome.substring(to: welcome.firstIndex(of: " ")!);
            print(welcome);
            
            let index = welcome.firstIndex(of: "l") ?? welcome.endIndex;
            let subWelcome = welcome[..

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