ch02基本操作符--Basic Operators

ch01基础


2.0赋值运算符--Assignment Operator

//let b = 10
//var a = 5
//a = b
// a is now equal to 10
//如果赋值运算符的右边是元组,元组可以被分解为多个常量或者变量
let (x,y) = (1,2)
// x is equal to 1, and y is equal to 2
//不行C和OC,swift里的赋值运算符本身没有返回值,下面的例子是非法的
//if x = y {
//    // this is not valid, because x = y does not return a value
//}
//这样可以阻止赋值运算符=和等于==相混,swift可以帮你避免这类错误

2.1算术运算符--Arithmetic Operators

//swift支持四种标准的算术运算符,+,-,*,/
//
1 + 2// equals 3
5 - 3// equals 2
2 * 3// equals 6
10.0 / 2.5// equals 4.0
//不像C和OC的算术运算符,swift里的算术运算符不允许值溢出
//另外一种运算符支持String
"hello, " + "world."// equals "hello, world"

2.2余运算符--Remainder Operator

9 % 4 // equals 1
//为了计算(a % b),%运算符会计算下面的表达式,并返回remainder
//a = (b x some multiplier) + remainder
//9 = (4 * 2) + 1
//同样的方法可以用做负数取余
-9 % 4  // equals -1
// -9 = (4 * -2) + -1
//说明: a % b和 a % -b,返回一样的值

2.3浮点余运算Floating-Point Remainder Calculations

//不像C和OC的算术运算符,swift里的余运算符可以用做浮点类型
8 % 2.5 // equals 0.5
// 8 = (2.5 * 3) + 0.5

2.4自增和自减运算符--Increment and Decrement Operators

//和C类似,swift也提供	++ 和 -- 运算符,任何整型或者浮点类型的变量都可以使用这种运算符
var i = 0
++i// i now equals 1
//说明:如果运算符写在变量前面,先改变变量值,在使用;如果运算符写在变量后面,先使用,在改变变量值
//var a = 0
//let b = ++a
 a and b are now both equal to 1
//let c = a++
// a is now equal to 2, but c has been set to the pre-increment value of 1
//说明:除非你有特别的需求使用i++,否则在所有的案列中推荐使用++i和--i,因为他们都是先修改值,在使用

2.5元减运算符--Unary Minus Operator

let three = 3
let minusThree = -three // minusThree equals -3
let plusThree = -minusThree // plusThree equals 3, or "minus minus three"

2.6元加运算符--Unary Plus Operator

//元加运算符仅仅返回值本身,无任何改变
let minusSix = -6
let alsoMinusSix = +minusSix// alsoMinusSix equals -6

2.7复合运算符--Compound Assignment Operators

var a = 1
a += 2// a is now equal to 3
//说明:复合运算符没有返回值,你不能写成这样 let b = a += 2


2.8比较运算符--Comparison Operators

//swift支持标志的C比较运算符:==,!=,>,<,>=,<=
//说明:swift还提供了两种运算符 === 和 !==主要用于判断2个对象是否是同一个实例
//比较运算符返回Bool值:true或者false
//比较运算符主要用在条件运算中,比如if语句
let name = "world"
if name == "world" {
    print("hello,world")
} else {
     print("I'm sorry \(name), but I don't recognize you")
}// prints "hello, world", because name is indeed equal to "world"


2.9三元条件运算符--Ternary Conditional Operator

//三元条件运算符的格式如下:question ? answer1 : answer2
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
// rowHeight is equal to 90


2.10nil合并运算符--Nil Coalescing Operator

//nil合并运算符(a ?? b)解开可选值a,如果a有值;或者返回一个默认的b,如果a为nil
//a总是一个可选optional类型
let defaultColorName = "red"
var userDefinedColorName: String?// defaults to nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName is nil, so colorNameToUse is set to the default of "red"
//如果给userDefinedColorName赋值,然后在用nil合并运算符,userDefinedColorName的值将用默认值代替
userDefinedColorName = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName is not nil, so colorNameToUse is set to "green"


2.11范围运算符--Range Operators

//封闭范围运算符
//封闭范围运算符(a...b)定义从a到b,包括a和b,a的值一定要比b小
//当你遍历所有值的时候,封闭范围运算符很有用,比如for循环
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
//半开区间运算符--Half-Open Range Operator
//半开区间运算符(a..<b)定义从a到b,但是不包括b,如果a等于b,结果将是空
//半开区间运算符主要用在以0开始的列表,比如数组
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
    print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack


2.12逻辑运算符--Logical Operators

//swift支持:非!a,与a && b ,或a || b
//!a
let allowedEntry = false
if !allowedEntry {
     print("ACCESS DENIED")
}
// prints "ACCESS DENIED"
//a && b
let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// prints "ACCESS DENIED"
// a || b
let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// prints "Welcome!"
//组合
if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// prints "Welcome!"


转载于:https://my.oschina.net/u/2493045/blog/631684

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值