最近在看swift寫的一個播放器demo代碼妓羊,順便學(xué)習(xí)下swift胯究。先把Demo放在下面,方便以后查找躁绸。
36kr原作者的Demo: VideoPlayer-網(wǎng)易播放器的風(fēng)格十分類似Demo
看代碼遇到不懂的查的幾個blog裕循,幾個博主說的都很清晰。列在下面净刮,方便記錄剥哑。
Swift惰性初始化(lazy)屬性
Swift中的willSet與didSet
Swift的Guard語句
Swift控制流: 如何理解 if let 與guard?
Swift之?和!
swift問題:
-
swift中的let和var什么區(qū)別?
“Use let to make a constant and var to make a variable.”
-----摘錄來自: Apple Inc. “The Swift Programming Language”淹父。 iBooks. ?和株婴!區(qū)別?
var strValue : String?
在變量后面加暑认?相當(dāng)于賦予一個初始值困介。
而!表示“我確定這里的的var一定是非nil的,盡情調(diào)用吧”,如果確定變量非nil,才可以使用蘸际,例如:
var stringValue : String?
//確定非nil座哩,就可以使用
if (stringValue != nil)
{
let hashValue = stringValue!.hashValue
}
//同樣可以使用optional banding ,代碼更加簡潔
if let str=stringValue
{
let hashValue = str.hashValue
}
- as是做什么用的?as?和as!
as操作符用來把某個實例轉(zhuǎn)型為另外的類型粮彤,由于實例轉(zhuǎn)型可能失敗根穷,因此Swift為as操作符提供了兩種形式:選項形式as?和強制形式as!
- willSet和didSet可以監(jiān)聽變量驾诈,非常有用的一個功能缠诅。參考博客:Swift中的willSet與didSet