The Swift Programming Language--Basic Operators

Basic Operators

  • Assignment Operator
    let (x, y) = (1, 2)

  • Arithmetic Operators
    let str = "Hello" + "World" // '+' operator

Remainder Operator. The sign of b is ignored for negative values of b. This means that a%b and a%-b always give the same answer.

  let remains = 8%2.5 // 0.5

++ and -- can be used for any integer and floating-point type.

  let minusSix = -6
  let alsoSix = +minusSix // -6, '+' does not do anything
  • Nil Coalescing Operator
    a ?? b // a is optional, b is the actual type of a
    a != nil ? a! : b

    let str: String = "Hello"
    var str1: String?// default set to nil
    
    var str2 = str1 ?? str // Hello, since str1 is nil
    
  • Range Operator
    for i in 1...5 {
    print(i)
    }//1 2 3 4 5

    for i in 1..<5 {
       print(i)
    }// 1 2 3 4
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,391评论 0 2
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,502评论 0 13
  • 今天我又提现二十多块钱,原来感觉只有够五十才能提现,今天尝试一下二十元是否能提现。 来简书一年多了,来简书散心成了...
    在听Yellow阅读 260评论 4 8
  • 在窗体的LOAD事件中加入代码: this.textBox1.ImeMode = ImeMode.Off; 在te...
    我是宁君阅读 1,323评论 0 0
  • ButterKnife现在最新10.1.0版本,先说下我现在的环境。AS版本:3.3.2,Gradle版本:4.1...
    JamFF阅读 25,802评论 8 28