Swift 5.0 問題
1.AnyObject 全部用Any替換
2.閉包需要自己寫參數(shù)
3.pod文件需加上use_frameworks!格式為
use_frameworks!
platform :ios, '8.0'
target "項(xiàng)目"do
pod 'AFNetworking'
pod 'SDWebImage'
pod 'SDAutoLayout'
end
4.Swift 中,在使用Any類型的屬性前,必須將其轉(zhuǎn)換一個(gè)明確的類型 as!/? 類型
5.iOS swift 3.0 后WKWebView 劫持點(diǎn)擊鏈接事件
? ? funcwebView(_webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler:@escaping(WKNavigationResponsePolicy) -> Void) {
//判斷是否為百度仰美,如果是則返回,不是則加載
if(navigationResponse.response.url?.host == "m.baidu.com"){
? ? ? ? ? ? //返回
? ? ? ? ? ? decisionHandler(.cancel)
}else{
? ? decisionHandler(.allow)
}
}
6.懶加載控件privatelazyvar 名字:屬性 =屬性(初始化)
7.Swift4.0后煞聪,字典轉(zhuǎn)模型用OC的KVC方法需要給屬性加上@objc 否則會(huì)奔潰
8.在extension中,只允許寫便利構(gòu)造函數(shù)念链,不允許寫指定構(gòu)造函數(shù)
9.單純的as 在 swift 中大多只有三個(gè)地方會(huì)用”橋接”
?? 1>String as NSSting
?? 2>NSArray as [array]
?? 3>NSDictionary as [String: AnyObject]
10.加載web網(wǎng)頁需要配置Plist文件如下
11.Swift-歸檔解檔存儲(chǔ)自定義類型數(shù)據(jù)
? ? ? 存儲(chǔ)操作(NSKeyedArchiver.archivedData)
? ? ? ? // 路徑
? ? ? ? let file = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
? ? ? ? // 拼接路徑 自動(dòng)帶斜杠的
? ? ? ? let filePath = (file as NSString).appendingPathComponent("UserAccount.archiver")
? ? ? ? print("用戶信息路徑:\(filePath)")
? ? ? ? // 保存
? ? ? ? do {
? ? ? ? ? ? let data = try NSKeyedArchiver.archivedData(withRootObject: "要存入的自定義類型的數(shù)據(jù)", requiringSecureCoding: true)
? ? ? ? ? ? do {
? ? ? ? ? ? ? ? _ = try data.write(to: URL(fileURLWithPath: filePath))
? ? ? ? ? ? ? ? print("寫入成功")
? ? ? ? ? ? } catch {
? ? ? ? ? ? ? ? print("data寫入本地失敗: \(error)")
? ? ? ? ? ? }
? ? ? ? } catch? {
? ? ? ? ? ? print("模型轉(zhuǎn)data失敗: \(error)")
? ? ? ? }
獲取操作(NSKeyedUnarchiver.unarchiveTopLevelObjectWithData)
? ? ? ? // 路徑
? ? ? ? let file = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last!
? ? ? ? // 拼接路徑 自動(dòng)帶斜杠的
? ? ? ? let filePath = (file as NSString).appendingPathComponent("UserAccount.archiver")
? ? ? ? do {
? ? ? ? ? ? let data = try Data.init(contentsOf: URL(fileURLWithPath: filePath))
? ? ? ? ? ? // 當(dāng)用戶首次登陸, 直接從沙盒獲取數(shù)據(jù), 就會(huì)為nil? 所以這里需要使用as?
? ? ? ? ? ? let model = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? "已經(jīng)存入的自定義類型的數(shù)據(jù)"
? ? ? ? ? ? return model
? ? ? ? } catch {
? ? ? ? ? ? print("獲取data數(shù)據(jù)失敗: \(error)")
? ? ? ? }