Swift开篇002->基本运算符

前端之家收集整理的这篇文章主要介绍了Swift开篇002->基本运算符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

PART_A 运算符

  1. 赋值运算符:

    • 普通赋值

      var num
      num = 5
    • 元组赋值

      let (x,y) = (0,0)
    • = 操作不返回任何值

    • @H_502_58@
    • 算数、组合赋值、自增自减、、负号、字符串拼接

      • 求余时符号只看左边,可对浮点数求余

        let a = 3,b = 4
         // 算数运算
        var c = 0
        c = a + b
        c = a - b
        c = a * b
        c = a / b
        c = a % b
         // 组合赋值运算
        c += a
        c -= a
        c *= a
        c /= a
        c %= a
         // 自增自减运算
        c++ // 先自增,再返回值
        c--
        ++c // 先返回值,再自增
        --c
         // 负号
        -c
         // 字符串拼接
        String str = "hello " + "world"
      • @H_502_58@
      • 比较运算符

        let a = 3,b = 4
        
        a == b
        a != b
        a > b
        a >= b
        a < b
        a <= b
      • ? : 三目运算符

        let num = 3
        num == 5 ? "yes" : "no"
      • ?? 空合运算符

        • 判断a,若为空则返回b值,若不为空解封并返回a值

        • a必须是Optional类型

        • b的存储值类型必须和a一致

          var a:Int?,b = 5
          a ?? b
          a != nil ? a! : b
        • @H_502_58@
        • 区间运算符

          • a ... b 闭区间

            • b必须大于a

            • 半开区间:a ..< b 或者 a >.. b

            • 数组遍历时:0 ..< strArr.count

            • @H_502_58@
              for x in 1 ... 9 {
                  for var y = 1; y <= x; y++ {
                      print("\(y) * \(x) = \(x * y)",terminator:"\t")
                      // 字符串插值:通过\()来插入实际值
                      // \()中不能包含非转义\、回车、换行符
                  }
                  print()
              }
              @H_502_58@
            • 逻辑运算

              • 非:!a

              • 与(短路):a && b

              • 或(短路):a || b

              • @H_502_58@
              • 推荐使用 () 来明确优先级


PART_B 字符串

字符串是值类型

  1. 初始化空字符串(两种方式等价)

    • var str = ""

    • var str = String()

    • @H_502_58@
    • 判空

      • str.isEmpty
      • @H_502_58@
      • 拼接

        • var str = "hi " + "catface"

          var str = "hello"
          let c : Character = "!"
          str.append(c) // str = "hello!"
        • @H_502_58@
        • 字符 Character

          • 初始化:let c : Character = "!"

          • 字符数组构造字符串

            let characters : [Character] = ["c","a","t","��"]
            let str = String(characters)
            print(str)
          • 遍历

            for c in "hello ~~ ☆".characters {
                print(c)
            }
            // h
            // e
            // ...
          • @H_502_58@
          • Unicode(国际标准,用于文本的编码和表示)

            • 字符串字面量的特殊字符

              特殊字符 含义
              \0 空字符
              \ 反斜线
              \t 水平制表符
              \n 换行符
              \r 回车符
              \” 双引号
              \’ 单引号
            • 可扩展的字形群集

              let c1 : Character = "\u{E9}"
              let c2 : Character = "\u{65}\u{301}"
              print("c1:\(c1),c2:\(c2),c1 == c2:\(c1 == c2)")
              // c1:é,c2:é,c1 == c2:true
              // 使用可拓展的字符群集改变字符串时,并不一定会更改字符串的字符数量
            • @H_502_58@
            • 访问和修改字符串

              • 索引

                let str = "hello world"
                print(str[str.startIndex])
                
                print(str[str.startIndex.successor()])
                print(str[str.endIndex.predecessor()])
                
                print(str[str.startIndex.advancedBy(3)])
                
                for index in str.characters.indices {
                    print(str[index],terminator : " ")
                }
                // h,e,d,l
                // h e l l o w o r l d 
                • starIndex:String的第一个Charater的索引

                • endIndex:String的最后一个Charater的索引,不是有效的字符串下标

                • successor():后一个索引

                • predecessor():前一个索引

                • advancedBy(index):指定位置的索引

                • indices:创建一个包含全部索引的范围(Range)

                • @H_502_58@
                • 插入和删除

                  • insert(_:atIndex:):指定索引插入字符

                  • insertContentsOf(_:at):指定索引插入字符串

                  • removeAtIndex(index)删除指定索引的值

                  • removeRange(range)删除指定索引的子字符串

                    var str = "hello world"
                    
                    str.insert("-",atIndex:str.startIndex) 
                    // str = "-hello world"
                    
                    str.insertContentsOf("===".characters,at: str.endIndex)
                    // str = "-hello world==="
                    
                    str.removeAtIndex(str.startIndex) 
                    // str = "hello world==="
                    
                    str.removeRange(str.endIndex.advancedBy(-3) ..< str.endIndex)
                    // str = "hello world"
                  • @H_502_58@ @H_502_58@
                  • 比较字符串

                    • 字符串/字符相等:==!=

                    • 前缀/后缀相等:hasPrefix(str)hasSuffix(str)

                      if str.hasPrefix("hello") {
                          if str.hasSuffix("world") {
                              print("yes")
                          }
                      }
                    • @H_502_58@

以上。如有错误和疑问,欢迎指正提出。 catface.wyh@gmail.com

猜你在找的Swift相关文章