
- 時(shí)隔1個(gè)月,終于下定決心把項(xiàng)目從2.2升級為Swift 3.0 ,花費(fèi)了10左右小時(shí),代碼量有5000多一點(diǎn), 本來0個(gè)警告,現(xiàn)在還有32個(gè)警告還沒有給消除呢。

我總結(jié)3.0與2.2區(qū)別
1.閉包注意回調(diào)函數(shù) + @escaping
@escaping與@non-escaping聲明用來修飾閉包的
在函數(shù)return后,閉包并不會(huì)被銷毀,它被持有了。因?yàn)檫@個(gè)閉包要在異步請求回來后才執(zhí)行,這時(shí)候函數(shù)已經(jīng)return了,為了能執(zhí)行,必須被其他對象持有,<b>這里需要注意循環(huán)引用</b>
@non-escaping:閉包在函數(shù)內(nèi)執(zhí)行完后,函數(shù)才返回,閉包銷毀.2.函數(shù) 名字 _
///根據(jù) 用戶查詢 他上傳的歌曲
class func QueryUserMusicNetGet(_ user: UserModel,net:@escaping (_ objects: [AnyObject]?, _ error: NSError?) -> ()){
//創(chuàng)建用戶
let user = AVObject(className: "_User", objectId: (user.objectID))
//創(chuàng)建查詢
let query = MusicNet.query()!
query.whereKey(LYMusicKeyUserList, equalTo:user)
MusicNet.baseNetGet(query) { (objects, error) in
net(objects, error)
}
}
3 系統(tǒng)屬性名字的變化
1.屬性名字開頭小寫
2.NS大量取消:Bundle,原來是NSBundle4.注意
-open 和 public 定義的 entity 允許被所有作用域(包括當(dāng)前模塊內(nèi)文件或是其他模塊文件)訪問;
-internal 作用范圍僅限在 entity 所定義的模塊內(nèi)部,其他模塊文件無法訪問。ps:默認(rèn) Access Control Level 為 Internal;
-fileprivate 作用范圍為當(dāng)前文件,因此一個(gè)文件內(nèi)定義多個(gè)類,某個(gè)類標(biāo)記為 fileprivate 之后,當(dāng)前模塊內(nèi)的其他文件無法訪問這個(gè)類,而當(dāng)前文件內(nèi)定義的其他類可以訪問;
-private 只允許當(dāng)前作用域訪問。
-
-open 只能應(yīng)用于類和類成員,與 public 的不同之處在于:
-public 以及其他 more restrictive 訪問級別只能在定義的模塊內(nèi)被繼承;
-public 以及其他 more restrictive 訪問級別只能在定義的模塊內(nèi)被重寫;
-open 則既可以在定義的模塊或是其他模塊內(nèi)被繼承或重寫5.函數(shù)有返回值必須接收一下(不接收有警告)
比如
_=navigationController?.popViewController(animated: true)
- 6.異步這個(gè)代碼警告
///異步執(zhí)行
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {
///回到主線程
DispatchQueue.main.async(execute: { () -> Void in
})
})
//下面的不警告
DispatchQueue.global().async {
// code
DispatchQueue.main.sync {
// 主線程中
}
}
- 7.坐標(biāo)位置
CGRectGetMaxX(cove.frame)
cove.frame.maxY
//更好使了
- SDWebImage用不成,錯(cuò)誤
imageView.sd_setImage(with: URL(string: newValue.userModel.headImageUrl), placeholderImage: UIImage(named: "Icon")) { (image, err, type, url) in
}
什么鬼一直報(bào)錯(cuò) Ambiguous use of 'sd_setImage
- 找到錯(cuò)誤了如下:
imageView.sd_setImage(with: URL(string: newValue.userModel.headImageUrl), placeholderImage: UIImage(named: "Icon"), options: SDWebImageOptions.retryFailed) { (image, error, type, url) in
weakSelf.userHeadBtn.setImage(image, for: UIControlState())
}
Swift,必須要帶參數(shù)的方法
媽的 歸檔有嚴(yán)重bug
不知道什么情況
///歸檔
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(musicName, forKey: "musicName")
aCoder.encodeDouble(progress, forKey: "progress")
}
///解襠
required init(coder aDecoder: NSCoder)
{
super.init()
musicName=aDecoder.decodeObjectForKey("musicName") as? String
progress=aDecoder.decodeDoubleForKey("progress")//這句話,就是崩潰。 取不出值納悶了。
}
最后解決方法的思路,就是所有歸檔都存字符串,先Double先轉(zhuǎn)成字符串,歸檔,
解檔:取出字符串來以后再轉(zhuǎn)成Double,記得強(qiáng)制解包。
///歸檔
func encode(with aCoder: NSCoder) {
aCoder.encode(musicName, forKey: "musicName")
aCoder.encode("\(progress!)", forKey: "progress")//這里記得加!要不然是可選的
}
///解襠
required init(coder aDecoder: NSCoder)
{
super.init()
musicName=aDecoder.decodeObject(forKey: "musicName") as! String
progress=Double(aDecoder.decodeObject(forKey: "progress") as! String)
}
修復(fù)所有我所遇到BUG,另外把警告全部給消除,用15小時(shí)左右了。

很爽
個(gè)人博客: http://www.liangtongzhuo.com