Swift笔记(一)之基本操作符(Basic Operators)

注:本人是做java开发,很多与swift做比较都是基于java,因此个别有歧义的说法都是基于java而言,

 

Swift操作符分3类:Unary,Binary,Ternary(分别是一元,二元,三元操作符)

• Unary operators operate on a single target (such as -a). Unary prefix operators appear immediately before their target (such as !b), and unary postfix operators appear immediately after their target (such as i++).

一元操作符号用于操作单个目标对象,有 - , !,++,--等

•Binary operators operate on two targets (such as 2 + 3) and are infix because they appear in between their two targets.

二元操作符用于操作2个目标对象,我们常见的有 +,-,*,/,%等的算数运算符

•Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).

三元操作符用于操作3个目标对象,官方文档提出到,Swift只有一个三元操作符,也就是条件操作符(下面会具体谈到)

与其他语言相似,Swift也包含众多语言都有 操作符,当然也有一些独特的新操作符号.下面就来分别介绍Swift的操作符

 Assignment Operator(赋值操作符)

 本身=很简单,此处举个swift有意思的例子,也是一个新型的功能:

If the right side of the assignment is a tuple with multiple values, its elements can be decomposed into multiple constants or variables at once:

let (x, y) = (1, 2) //注:let用于定义一个常量,相当于JAVA中的final.

大家看到 = 右边的(1,2)了吧,官方解释到:右边赋值的是一个元组,包含2个值,这个元组可以分解成2个常量或者是变量.文档说到(x is eaual to 1,and y is equal 2);也就是把1赋值给了x,把2赋值给了y.

此处官方给c和oc程序员提了一个醒.

Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid:

if x = y {
    // this is not valid, because x = y does not return a value
}

在Swift中=操作符是不会返回值的,所以在比较两个是否相等时是不能使用 = ,需要使用到 == .但是,作者又给我们埋下了一个伏笔.如果=右边是一个Optional(可选的),那么就 x=y就成立.

 Arithmetic Operators(算数运算符)

算数运算符在各大编程语言都如出一辙,不过要提一点的就是 + 号也是连接符,可以使2个字符串或者字符与字符串,2个字符之间进行拼接,产生一个新的字符串.

在这里对于没有编程语言的人提一个醒: 3/2=1而不是=1.5

Increment and Decrement Operators(自增,自减操作符)

这个东西算的上是一个奇葩了.好多面试官总喜欢拿这个考人.真的算得上是蛋疼的一逼.我觉得这个本身就应该废除了,追求简洁的python,golang,都已经废除该操作符.就不多讲解了,曾经也是把我难道过的,其实细心点就OK了.可以查看http://blog.csdn.net/x369201170/article/details/8604601

Compound Assignment Operators(混合赋值操作符)英语不好,不知道这翻译对不

v ar  a = 1

a += 2

// a is now equal to 3

简单的说a+=2就是a=a+2另一种写法。再对某个变量做赋值并运算时,这种操作符是很有用的,有利于代码的简写,效率我没研究过.

官方文档提到,符合赋值操作符是不返回值的,是不能这样写:let b = a+=2  .这种行为与自增自减操作符不同。

Ternary Conditional  Operator(三元条件运算符)

三元操作运算符是一个比较有用的操作符,能极大的简写if else条件语句.来我们来看看:

曾经:

if question {
     answer 1
} else {
     answer 2
}
现在:

question?answer1:answer2
我们看到question在?前面,代表一个条件,如果这个条件为true那么执行answer1,反之为answer2;并且会返回该结果,那么我们这样
let answer = question?answer1:answer2

Range Operators(区间操作符)

Closed Range Operators(闭区间)

这个东西比较有趣,是我在JAVA中从来没看见过的

The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b.
The closed range operator is useful when iterating over a range in which you want all of the values to be used, such as with a for-in loop:
1 for index in 1...5 {
2     println("\(index) times 5 is \(index * 5)")
3 }
4 // 1 times 5 is 5
5 // 2 times 5 is 10
6 // 3 times 5 is 15
7 // 4 times 5 is 20
8 // 5 times 5 is 25
For more on for-in loops, see Control Flow.


官方提到,这个全闭区间操作符a...b定义了一个a到b的区间,从a到b并包括b.(例如:1-4,那么这个区间就包含1,2,3,4).区间操作符号也可以用于迭代中..上面有一个例子

Half-Closed Range Operator(半闭区间)

数学好的人都知道,有闭区间也有开区间(我数学很差).Swift也有半闭区间.

The half-closed range operator (a..b) defines a range that runs from a to b, but does not include b. It is said to be half-closed because it contains its first value, but not its final value.
Half-closed ranges are particularly useful when you work with zero-based lists such as arrays, where it is useful to count up to (but not including) the length of the list:
1 let names = ["Anna", "Alex", "Brian", "Jack"]
2 let count = names.count
3 for i in 0..count {
4     println("Person \(i + 1) is called \(names[i])")
5 }
6 // Person 1 is called Anna
7 // Person 2 is called Alex
8 // Person 3 is called Brian
9 // Person 4 is called Jack
Note that the array contains four items, but 0..count only counts as far as 3 (the index of the last item in the array), because it is a half-closed range. For more on arrays, see Arrays.


官方提到,这个半闭区间操作符a..b定义了一个a到b的区间,从a到b不包括b(例如:1-4,那么这个区间就包含1,2,3,4).作者给我们提到一个很好的建议,如果你要操作的数组是一个以0下标开始的.那么半闭区间是一个不错的选择,上面有一个例子很形象反应出这个.

还剩下几个逻辑运算符,我就不再多活了,主流语言都是一致的.

你可能感兴趣的:(Swift)