2.Swift-基本运算符

Swift支持大部分标准C语言的运算符, 且改进许多特性来减少常规编码错误. 如, 赋值符 = 不返回值, 以防止错把等号 == 写成赋值号 = 而导致Bug. 数值运算符( + , -, *, /, %等)会检测并不允许值溢出, 以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果. 当然允许你选择使用Swift的溢出运算符来玩溢出. 具体使用请移步溢出运算符.

 

区别于C语言, 在Swift中你可以对浮点数进行取余运算( % ), 还提供了C语言没有的表达两数之间的值的区间运算符, ( a ..< b 和 a ... b ), 这方便我们表达一个区间内的数值.(注意 ..< 在之前的苹果文档中是 .. ,后来苹果更新了, ..< 易读性更高!)


1.取余运算符

//取余可以用浮点数

var aFloatValue = 1.0%2.0 //1.0


2.区间运算符

//区间运算符---不包含上界

var sumOfNotClose = 0

for i in 0 ..< 4{

    sumOfNotClose++

}

sumOfNotClose   //4


//区间运算符---包含上界

var sumOfClose = 0

for i in 0 ... 4{

    sumOfClose++

}

sumOfClose      //5



基本数值运算符跟其他语言跟其他语言中的数值运算符基本上一样,不过


3.+运算符可以用于字符串拼接操作

var aStr = "Hello" + " everyone"



单目负号-、单目正号+、复合赋值(+=、-=)、比较运算符、三目条件运算符、逻辑运算符等跟其他语言基本没有差别,就省略介绍。



4.nil合并运算符 a ?? b(这是苹果后来加的)

a ?? b  首先a和b的类型要一样,其次a是可选类型,如果a为nil,那么就把默认值b赋值给a,否则a保持它原有的值。

是 a ! = nil a! : b的一种简写。

//nil合并运算

let defaultColorName = "red"

var userDefinedColorName:String?  //默认是nil

var colorNameToUse = userDefinedColorName ?? defaultColorName  //red


nil合并运算符非常简洁,易读性也非常好。



总结:

苹果的基本运算符跟其他语言包括OC没有太大的区别,不同点总结如下:

1. 赋值符 = 不返回值, 以防止错把等号 == 写成赋值号 = 而导致Bug。从源头上减少了Bug的数量。就像代码中可以不写分号一样。

2.取余运算%可以对浮点数进行取余了。

3.新增的区间运算符..<和... ,在循环中使用非常的方便,而且易读性非常好,越来越人性化了。

4.+运算符可以很方便的对字符串进行拼接操作了。

5.nil合并运算符对三元运算符进行了进一步简化。


参考:

1.The Swift Programming Language

2.The Swift Programming Language--语言指南--基础部分

注意:以苹果官方的The Swift Programming Language文档为主!因为我发现苹果有时候会有一些小小的改动,比如 ..< 运算符之前是 .. 运算符,下面的cocoachina的文章翻译的时候还是 ..运算符。此外苹果还新增了nil合并运算符 a ?? b。所以说,还是要时常关注苹果官方的文档更新。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接着分析 (result (type_ident (component id='Bool' bind=Swift.(file).Bool))) (brace_stmt range=[re.swift:1:59 - line:14:1] (pattern_binding_decl range=[re.swift:2:5 - line:2:33] (pattern_named type='[UInt8]' 'b') Original init: (call_expr type='[UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:33] nothrow (constructor_ref_call_expr type='(String.UTF8View) -> [UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:19] nothrow (declref_expr implicit type='(Array<UInt8>.Type) -> (String.UTF8View) -> Array<UInt8>' location=re.swift:2:19 range=[re.swift:2:19 - line:2:19] decl=Swift.(file).Array extension.init(_:) [with (substitution_map generic_signature=<Element, S where Element == S.Element, S : Sequence> (substitution Element -> UInt8) (substitution S -> String.UTF8View))] function_ref=single) (argument_list implicit (argument (type_expr type='[UInt8].Type' location=re.swift:2:13 range=[re.swift:2:13 - line:2:19] typerepr='[UInt8]')) )) (argument_list (argument (member_ref_expr type='String.UTF8View' location=re.swift:2:29 range=[re.swift:2:21 - line:2:29] decl=Swift.(file).String extension.utf8 (declref_expr type='String' location=re.swift:2:21 range=[re.swift:2:21 - line:2:21] decl=re.(file).check(_:_:).encoded@re.swift:1:14 function_ref=unapplied))) )) Processed init: (call_expr type='[UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:33] nothrow (constructor_ref_call_expr type='(String.UTF8View) -> [UInt8]' location=re.swift:2:19 range=[re.swift:2:13 - line:2:19] nothrow (declref_expr implicit type='(Array<UInt8>.Type) -> (String.UTF8View) -> Array<UInt8>' location=re.swift:2:19 range=[re.swift:2:19 - line:2:19] decl=Swift.(file).Array extension.init(_:) [with (substitution_map generic_signature=<Element, S where Element == S.Element, S : Sequence> (substitution Element -> UInt8) (substitution S -> String.UTF8View))] function_ref=single) (argument_list implicit (argument (type_expr type='[UInt8].Type' location=re.swift:2:13 range=[re.swift:2:13 - line:2:19] typerepr='[UInt8]')) )) (argument_list (argument (member_ref_expr type='String.UTF8View' location=re.swift:2:29 range=[re.swift:2:21 - line:2:29] decl=Swift.(file).String extension.utf8 (declref_expr type='String' location=re.swift:2:21 range=[re.swift:2:21 - line:2:21] decl=re.(file).check(_:_:).encoded@re.swift:1:14 function_ref=unapplied))) ))) (var_decl range=[re.swift:2:9 - line:2:9] "b" type='[UInt8]' interface type='[UInt8]' access=private readImpl=stored writeImpl=stored readWriteImpl=stored)
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值