Swift3.0 - 真的很簡單
Swift3.0 - 數(shù)據(jù)類型
Swift3.0 - Array
Swift3.0 - 字典
Swift3.0 - 可選值
Swift3.0 - 集合
Swift3.0 - 流控制
Swift3.0 - 對象和類
Swift3.0 - 屬性
Swift3.0 - 函數(shù)和閉包
Swift3.0 - 初始化和釋放
Swift3.0 - 協(xié)議protocol
Swift3.0 - 類和結(jié)構(gòu)體的區(qū)別
Swift3.0 - 枚舉
Swift3.0 - 擴(kuò)展
Swift3.0 - 下標(biāo)
Swift3.0 - 泛型
Swift3.0 - 異常錯誤
Swift3.0 - 斷言
Swift3.0 - 自動引用計數(shù)(strong,weak,unowned)
Swift3.0 - 檢測API
Swift3.0 - 對象的標(biāo)識
Swift3.0 - 注釋
Swift3.0 - 元類型
Swift3.0 - 空間命名
Swift3.0 - 對象判等
Swift3.0 - 探究Self的用途
Swift3.0 - 類簇
Swift3.0 - 動態(tài)調(diào)用對象(實例)方法
Swift3.0 - 文本輸出
Swift3.0 - 黑魔法swizzle
Swift3.0 - 鏡像
Swift3.0 - 遇到的坑
- 文本方式輸出對象
class Person:CustomStringConvertible {
var name: String
var age: Int
var weight:Double
init(name:String,age:Int,weight:Double) {
self.name = name
self.age = age
self.weight = weight
}
var description: String{
return name + String(age) + String(weight)
}
}
let person = Person(name: "酷走天涯", age: 25,weight:150.0)
// 獲取對象文本
let des = String(describing: person)
print(des)
運行結(jié)果:
酷走天涯25150.0
提示:
讓對象文本輸出很簡單盖矫,只要實現(xiàn)協(xié)議CustomStringConvertible 即可
思考: 下面的代碼為什么不正確
class Person:NSObject,CustomStringConvertible {
var name: String
var age: Int
var weight:Double
init(name:String,age:Int,weight:Double) {
self.name = name
self.age = age
self.weight = weight
}
var description: String{
return name + String(age) + String(weight)
}
}
提示:
NSObject 里面已經(jīng)實現(xiàn)了協(xié)議CustomStringConvertible,不能重復(fù)實現(xiàn),我們只需要重寫var description: String 即可
正確代碼如下:
class Person:NSObject {
var name: String
var age: Int
var weight:Double
init(name:String,age:Int,weight:Double) {
self.name = name
self.age = age
self.weight = weight
}
override var description: String{
return name + String(age) + String(weight)
}
}