一 、在Playground中使用标签(在项目中未测试)
//:### 单行注释------test
/*:
### 多行注释 ----test
*/
在Xcode->Editor->Show Raw Markup
二、查看swift中类型
let blinfgHeart = "\u{1F496}"
blinfgHeart.dynamicType//-->xx.dynamicType
三、打印不换行
for codeunit in blinfgHeart.utf8
{
//-------terminator:""
print("\(codeunit)",terminator:"")
}
四、Nil Coalescing Operator
opt != nil ? opt! : b --->如果opt 为空 就读取默认值 否则就另外表达式的值
var userInput:String?
let value = userInput ?? "a default input"//value = "a default input"
var userInput:String? = "a user input"
let value = userInput ?? "a default input"// value = "a user input"
五、enumerate函数来遍历数组
同时返回数据项和索引值
for (index,Value) in fiveInts.enumerate()
{
print("Index:\(index),Value:\(Value)")
}
打印结果:
**Index:0,Value:1**
**Index:1,Value:2**
**Index:2,Value:3**
**Index:3,Value:4**
**Index:4,Value:5**
六、字典与数组
字典数组是否为空
capotalNumber.isEmpty//返回BOOL
让字典capotalNumber的key按照从小到大的排序
for number in capotalNumber.keys.sort()
{
number
}
capotalNumber.keys
获取到字典中key并依次放进数组中
let keyArray = [Int](capotalNumber.keys)
获取到字典中value并依次放进数组中
let valueArray = [String](capotalNumber.values)
七、Switch在swift上的修改
tuple
let point = (1,1)
switch point{
case (0,0):
print("point is at the x-axis")
case(_,0):
print("(\(point.0),0) is on the x-axis")
case(0,_):
print("(0,\(point.1)) is on the y-axis")
case (-2...2,-2...2):
print("(\(point.0),\(point.1) is inside the blue box")
default:
print("(\(point.0),\(point.1) is outside the blue box")
}
Value binding
switch point{
case (let x,0):
print("with the X value of:\(x)")
case (0,let y):
print("with the Y value of:\(y)")
case (let x, let y):
print("X:\(x)Y :\(y)")
}
switch point{
case let(x,y) where x == y:
print("x==y")
case let(x,y) where x == -y:
print("x== -y")
case let(x,y):
print("\(point.0,point.1)")
}
七、函数
_ 可以替换outName 让函数调用跟C一样
func multipleOfs(multiplier: Int,andValue:Int)
{
print("\(multiplier)*\(andValue) = \(multiplier * andValue)")
}
multipleOfs(10, andValue: 5)
func multipleOf(multiplier: Int,_ andValue:Int)
{
print("\(multiplier)*\(andValue) = \(multiplier * andValue)")
}
multipleOf(10, 5)
innername outname 让函数调用更加易读
func createTable(rowNumber row : Int,colNumber column: Int)
{
print("Table:\(row) X \(column)")
}
createTable(rowNumber: 10, colNumber: 10)
intout param
func increment(inout vlaue: Int)
{
vlaue += 1
}
var m = 10
increment(&m)
print("\(m)")//m ==11
八、复合函数和函数返回
var f4:(Int,Int) -> Int = mutipleOf//第七点用到的函数
fnparam是fn函数的参数 ectecute调用名为fn的函数
func ectecute(fn:(String) -> Int? ,_ fnParam:String ){
fn(fnParam)
}
-----------------------------------------
-----------------------------------------
func increment(n: Int) -> Int {
return n+1
}
func decrement(n: Int) -> Int {
return n - 1
}
typealias op = (Int) -> Int
func whichOne(n:Bool) -> op {
return n ? increment : decrement
}
var one = 1
var oneToTen = whichOne( one < 10)
while one < 10
{
one = oneToTen(one)
}
🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽 🔽
typealias ops = (Int) -> Int
func whichOnes(n:Bool) -> ops {
func increments(n: Int) -> Int {
return n+1
}
func decrements(n: Int) -> Int {
return n - 1
}
return n ? increment : decrement
}
九、匿名函数
//:### 标准初始化
//{}标示 closure 全部内容
var addClosure:(Int,Int) -> Int = {
(a:Int ,b:Int) -> Int in
return a + b
}
addClosure (5,10)
addClosure = {a,b in return a + b}
//: Single expression closure
addClosure = {a,b in a+b}
=================================
=================================
func execute(a:Int,_ b: Int,operation:(Int,Int)-> Int) -> Int {
return operation(a,b)
}
func addFunc(a:Int ,_ b : Int) -> Int {
return a + b
}
execute(1,10,operation:addFunc)
execute(1, 10, operation: addClosure)
execute(1, 10, operation: {(a:Int,b:Int)->Int in
return a + b
})
execute(1, 10, operation: {a,b in a + b })
execute(1,10, operation: {$0 + $1})
execute(1, 10) {$1 + $0}//Tailing Closure
==================================
==================================
//:###Capturing value
//调用一个()空函数 返回Int
func counting()->()->Int{
var count1 = 0
let incrementCount:()->Int = {
count1++;
}
return incrementCount
}
十、谨慎处理的“空”值--Optional
nil 只能赋值给optional
var X:Int?
var address :String?
var successRate:Double?
//判断 converResult不为空 之后通过converResult!取值
if converResult != nil
{
print( converResult!)
let sum = converResult! + 1
}
if let numaber = converResult
{
print(numaber)
}
else
{
print("Convert result is nil")
}
if var number = converResult{
number += 1
print(number)
print(converResult)
let sum = number + 1
}
else{
print("Conver result is nil")
}
Implicitly Unwrapped Optional
//: possibleString 一定不能为空
var possibleString :String! = "A dangerous way !"
print(possibleNumber)
possibleNumber + "use it with caution"
十一、Optional Chaining
enum Type{
case CREDIT
case DEPOSIT
}
class BankCard{
var type:Type = .CREDIT
}
class Person {
var card = BankCard?()
init(card:BankCard? = nil)
{
self.card = card
}
}
let nilPerson:Person? = nil
let noCardPerson: Person? = Person()
let creditCardPrerson: Person? = Person(card : BankCard())
nilPerson?.card?.type
noCardPerson?.card?.type
creditCardPrerson?.card?.type
nilPerson.flatMap({$0.card}).flatMap({$0.type})
noCardPerson.flatMap({$0.card}).flatMap({$0.type})
creditCardPrerson.flatMap({$0.card}).flatMap({$0.type})
文章写得不是很完整会持续更新,如果有缺陷的地方还请指出不胜感激。
网友评论