一、基本运算符
let a = 5
var b = 10
b = a
if a = b{
swift 中赋值运算符,并不将自身作为一个值进行返回,所以编译不合法,帮开发者避免错误,很人性化的语言
}
二、数学运算符
let dog:Character = "??"
let cow:Character = "??"
let dogCow = dog + cow
println(dogCow)
// swift 的算术运算符 不允许溢出,通过溢出运算符来选择值的溢出情况(a& + b)
// 余数运算符 可以用于 浮点数 flost
8 % 2.5 // equals 0.5
// 一元减运算符
let three = 3
let minusTree = -three
let plusThree = -minusTree
println(minusTree)
println(plusThree)
// 一元加运算符
三、范围运算符
=== !== 两个恒等运算符 检测两个对象引用是否来自于同一个对象实例
两个范围
封闭(a...b)表示:a到b的值
for index in 1...5{
println()
}
半封闭
(a..b),包头不包尾
let names = ["anna","alex","bbsc","sdfs"]
let count = names.count
for i in 0...count-1{
println("person \(i) is called \(names[i])")
}
四、字符和字符串
快捷键:
option +Y = "Y"
emoji = control + commod +space
swift中的字符串不是指针 是实际的值
初始化空字符串
var emptyString = ""
var anotherEmptyStr = String()
if emptyString.isEmpty{
}
var variableString = "horse"
variableString += " and carriage"
countElements 计算字符串中的字符数量
let countStr = "hahahh"
println(countStr count is \(countElements(countStr))")
NSString 的length 是基于UTF-16编码的数目 而不是基于Unicode
swift中字符串并不一定占用相同的内存空间。
字符串比较
let someStr1 = "abc"
let someStr2 = "abc"
if someStr1 == someStr2{
}
swift中的字符串 不是指针 是实际的值
前缀相等
let animals = ["食肉:老虎","食肉:狮子","食草:羊","食草:牛","食草:马"]
var aCount = 0
for animal in animals{
if animal.hasPrefic("食肉"){
++aCount
}
}
println("这里有\(aCount)头食肉动物")
转换字符串大小写
let normal = "could you help me"
let shoty = normal.uppercaseString //大写
let whispered = normal.lowercaseString //小写
字符串编码 输出 都是使用C语言的 print
.utf8 .utf16
unicode标量
.unicodeScalars
输出时要放个空格
for scalar in dogString.unicodeScalars
{
print("\(scalar)" )
}