swift項目工程搭建
參考:
http://www.reibang.com/p/f324c2e95b34
https://blog.csdn.net/qq_33298465/article/details/74451765
swift常用框架
- 參考:
http://www.reibang.com/p/fbe0806549b6
https://blog.csdn.net/zWbKingGo/article/details/109514983
https://blog.csdn.net/zWbKingGo/article/details/109514983
SnapKit
自動布局三方庫嚼酝,類似于OC的Masonry。
swift運行問題
The run destination hh的iPhone is not valid for Running the scheme 'appdemo'.
hh2的iPhone’s iOS 14.1 doesn’t match appdemo’s iOS 15.2 deployment target. Upgrade hh的iPhone’s iOS version or lower appdemo’s deployment target.
- 處理:
全局找到所有deployment的部分拔第,設(shè)置版本為13以上(因為最新的工程都加入了UIScene雌续,13.0才出來的)
Could not launch “appdemo”
The operation couldn’t be completed. Unable to launch com.app.demo because it has an invalid code signature, inadequate entitlements or its profile has not been explicitly trusted by the user.
- 處理:需要在手機設(shè)置-通用-設(shè)備管理:信任開發(fā)者賬號
swift error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
- 參考:
https://blog.csdn.net/xjh093/article/details/80830458
https://blog.csdn.net/dongtinghong/article/details/51579888
swift UITabBarController
- 參考:
http://www.reibang.com/p/52ec78e1229a
http://www.reibang.com/p/d1380241c4cf
https://jingyan.baidu.com/article/ea24bc39e64a90da62b3313b.html
swift UITabBarController再push一個頁面會被tabbar遮擋
- 設(shè)置hidesBottomBarWhenPushed為YES即可音五。
- 參考:
https://blog.csdn.net/u011452278/article/details/52858910
swift問題
Initializer for conditional binding must have Optional type, not 'MineModelData'
Property 'self.label' not initialized at super.init call
Cannot find 'MineReuseIdentifier' in scope
Value of type 'UITableViewCell' has no member 'updateCell'
- 原因:UITableViewCell使用不對
Alamofire
網(wǎng)絡(luò)請求三方庫,類似于OC的AFNetwork
參考:
http://www.reibang.com/p/b7174ed30901
http://www.zzvips.com/article/129480.html
swift extension
- 參考:
http://www.reibang.com/p/783df05a9b59 - extension:與OC分類/擴展區(qū)別
參考:
http://www.reibang.com/p/120d3d3446dd
swift class/struct
-
struct和class區(qū)別
相同點:
1.定義屬性用于存儲值 (property)
2.定義方法用于提供功能 (function)
3.定義下標操作使得可以通過下標語法來訪問實例所包含的值 (subscript)
4.定義構(gòu)造器用于生成初始化值 (initializers)
5.通過擴展以增加默認實現(xiàn)的功能 (extension)
6.實現(xiàn)協(xié)議以提供某種標準功能 (protocol)不同點:
1.class是引用類型;struct是值類型
2.class支持繼承淳梦;struct不支持繼承
3.class聲明的方法修改屬性時不需要mutating關(guān)鍵字;struct需要
4.class中每一個成員變量都必須被初始化昔字,否則編譯器會報錯爆袍,而struct不需要,編譯器會自動幫我們生成init函數(shù)作郭,給變量賦一個默認值
5.class支持引用計數(shù)(reference counting)(允許對一個類的多次引用)陨囊,struct不支持
6.class支持type casting(類型轉(zhuǎn)換)(允許在運行時檢查和解釋一個類實例的類型),struct不支持
7.class支持deinitializers(析構(gòu)器)(允許一個類實例釋放任何其所被分配的資源)夹攒,struct不支持
8.變量賦值方式不同(深淺copy)蜘醋,class淺拷貝,struct深拷貝咏尝,class的賦值是傳遞引用压语,struct則是copy傳值,不是使用引用計數(shù)啸罢。
9.內(nèi)存管理:struct存儲在stack中,class存儲在heap中胎食、
10.方法派發(fā)方式:struct的方法調(diào)用是靜態(tài)綁定扰才,而class的方法調(diào)用是動態(tài)實現(xiàn)
swift public/private/open
swift public/private/open/internal/fileprivate
Swift文件權(quán)限:open/public/internal/private/fileprivate區(qū)別,默認權(quán)限internal
1)private 訪問級別所修飾的屬性或者方法只能在當前類里訪問厕怜。
2)fileprivate 訪問級別所修飾的屬性或者方法在當前的 Swift 源文件里可以訪問训桶。
3)internal 訪問級別所修飾的屬性或方法在源代碼所在的整個模塊都可以訪問。
如果是框架或者庫代碼酣倾,則在整個框架內(nèi)部都可以訪問舵揭,框架由外部代碼所引用時,則不可以訪問躁锡。
如果是 App 代碼午绳,也是在整個 App 代碼,也是在整個 App 內(nèi)部可以訪問映之。
4)public可以被任何人訪問拦焚。但其他 module 中不可以被 override 和繼承,而在 module 內(nèi)可以被 override 和繼承杠输。
5)open可以被任何人使用赎败,包括 override 和繼承。參考:
https://blog.csdn.net/chenyong05314/article/details/116267430
getTopViewController
swift宏定義
swift @available / #available
參考:
http://www.reibang.com/p/eef6c563a1ea
https://blog.haohtml.com/archives/16972注意:
用于語句:#available
用于函數(shù)/類/協(xié)議:@available
swift: Type of expression is ambiguous without more context
UIApplication.shared.windows.statusBarManager?.statusBarFrame.size.height
swift: cast error
Cast from 'AnyClass?' (aka 'Optional<AnyObject.Type>') to unrelated type 'ViewController' always fails
swift: compiler error
error: Segmentation fault: 11 (in target 'app-demo-ios' from project 'app-demo-ios)
說明:之前有對Legacy Build System進行配置的方案蠢甲。但是目前該方案已經(jīng)不適用了僵刮。
參考:
https://blog.csdn.net/pjf_1806339272/article/details/107498775
: The Legacy Build System will be removed in a future release. You can configure the selected build system and this deprecation message in File > Workspace Settings.
處理:這個地方跟Legacy Build System設(shè)置沒有關(guān)系。
是和變量定義和if let等判斷錯誤有關(guān)鹦牛。
swift No exact matches in call to initializer
Initializer for conditional binding must have Optional type, not 'ViewController'
- 參考:
http://www.reibang.com/p/406ebbfc0dfc
https://blog.csdn.net/qq_15623599/article/details/88969525
swift as:swift強制類型轉(zhuǎn)換
Cannot convert value of type 'SettingsViewController' to type 'ViewController' in coercion
- 參考:
https://blog.csdn.net/wufeifan_learner/article/details/89074298
https://www.runoob.com/swift/swift-type-casting.html
swift lazy
swift button
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSLayoutConstraint for <UIButton: 0x104f06110; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x2820b4d80>>: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs.'
- 原因:按鈕初始化時未給frame
swift 獲取safearea
- 參考:
https://wenku.baidu.com/view/d18e1fa5e63a580216fc700abb68a98271feace9.html
https://blog.csdn.net/weixin_30632883/article/details/96044690
swift ==和===
說明:===除了會比較值以外搞糕,還會比較引用地址
參考:
https://blog.csdn.net/beyondforme/article/details/106366102
UIWindow:swift添加到UIWindow
參考:
https://blog.csdn.net/weixin_30567471/article/details/97128540swift AppDelegate添加view不顯示
參考:
http://www.reibang.com/p/984b56f09261
http://www.reibang.com/p/a14550af3e89最終處理方案:
[[UIApplication sharedApplication].keyWindow addSubview:label];
swift dispatch
swift/oc
swift與oc區(qū)別
1.swift是靜態(tài)語言,有類型推斷曼追,OC是動態(tài)語言窍仰。
2.swift面向協(xié)議編程,OC面向?qū)ο缶幊?br> 3.swift注重值類型礼殊,OC注重引用類型驹吮。
4.swift支持泛型,OC只支持輕量泛型
5.swift支持靜態(tài)派發(fā)(效率高)晶伦、動態(tài)派發(fā)(函數(shù)表派發(fā)碟狞、消息派發(fā))方式,OC支持動態(tài)派發(fā)(消息派發(fā))方式坝辫。
6.swift支持函數(shù)式編程
7.swift的協(xié)議不僅可以被類實現(xiàn)篷就,也可以被struct和enum實現(xiàn)
8.swift有元組類型、支持運算符重載
9.swift支持命名空間
10.swift支持默認參數(shù)
11.swift比oc代碼更加簡潔
keyWindow
'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes
- 參考:
https://blog.csdn.net/yst19910702/article/details/108880067
https://devnote.pro/posts/10000051421241
Alamofire:響應(yīng)日志
sy.ak: login response: failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
處理:使用responseDecodable
Generic parameter 'T' could not be inferred
無法推斷泛型參數(shù)'T'
網(wǎng)絡(luò)連接:日志
sy.ak: login response.result: failure(Alamofire.AFError.sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLStringKey=http://upgrade.31truck.com:5247/pkg/check?version=13.2.16&os=Android&env=development&bundleId=com.app.demo&containerVersion=13.2.16, NSErrorFailingURLKey=http://upgrade.31truck.com:5247/pkg/check?version=13.2.16&os=Android&env=development&bundleId=com.app.demo&containerVersion=13.2.16, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <2215D0AC-BF6A-4543-AB54-B64A0A9ACE72>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <2215D0AC-BF6A-4543-AB54-B64A0A9ACE72>.<2>, NSUnderlyingError=0x2818a6160 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}}))
原因:應(yīng)該是沒有在info.plist中申請網(wǎng)絡(luò)權(quán)限
處理:
App Transport Security Settings
設(shè)置Allow Arbitrary Loads為YES
import Kingfisher
圖片處理庫
crypto導(dǎo)入swift
參考:
https://wenku.baidu.com/view/6842a06b2d3f5727a5e9856a561252d380eb20cd.html
https://www.sohu.com/a/417205679_99956743
https://www.csdn.net/tags/MtTaEg3sMDYwMzQ1LWJsb2cO0O0O.htmlswift crypto
參考:
https://codingdict.com/os/software/50757
https://blog.csdn.net/chechengxue/article/details/109308418CryptoSwift介紹
參考:
https://github.com/krzyzanowskim/CryptoSwiftswiftCrypto使用
參考:
https://www.hangge.com/blog/cache/detail_1865.html
swift Digest
Xcode remove Package Dependencies
swift WKWebView
swift輪播圖
- 參考:
https://blog.csdn.net/mp624183768/article/details/108601692
http://www.reibang.com/p/776d1333717a
http://www.reibang.com/p/9b72dd21d222
PageMenuView:swift可以來回切的有tab的view
參考:
https://wenku.baidu.com/view/cab9d4fb5322aaea998fcc22bcd126fff7055d61.html
http://www.reibang.com/p/aae3299d2d0d?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation參考:PagingMenuController github
https://blog.csdn.net/NoPolun_iOS/article/details/84708153
UIViewController 初始化錯誤
Must call a designated initializer of the superclass 'UIViewController'
處理:
需要調(diào)用UIViewController的初始化方法
定義屬性時 初始化錯誤
Property 'self.widthConstraint' not initialized at super.init call
處理:有兩種方式
一是:在調(diào)用super.init之前設(shè)置self.widthConstraint
二是如下定義:
fileprivate var widthConstraint: NSLayoutConstraint!
注意:第二種方式最后有一個感嘆號,如果沒有就會報上面的錯誤
swift布局:錯誤
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x280405780 "app_demo_ios.MenuView:0x108014e00.top"> and <NSLayoutYAxisAnchor:0x280407800 "UIView:0x10780a000.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
原因:在布局錨點時首先要把該view添加上來竭业。如下:
view.addSubview(menuView)參考:
http://www.reibang.com/p/5adc8a387bf5
https://www.e-learn.cn/content/wangluowenzhang/1026300
SwiftyJSON
- 參考:
https://wenku.baidu.com/view/1229f04ef6335a8102d276a20029bd64783e627e.html
https://wenku.baidu.com/view/43725dfb5322aaea998fcc22bcd126fff7055db8.html
https://wenku.baidu.com/view/3a70eb37ff4ffe4733687e21af45b307e871f912.html
https://blog.csdn.net/kicinio/article/details/111873330
git
- 參考:
http://www.srcmini.com/38487.html
https://github.com/SwiftyJSON/SwiftyJSON
http://www.reibang.com/p/e85c6e12a539
http://www.reibang.com/p/293c16132d0a
http://www.reibang.com/p/5fe6330ee84a
http://t.zoukankan.com/metaphors-p-9405432.html
swift DataResponse
response.value:Optional(<__NSArrayI 0x281146730>(
escaping
Escaping closure captures non-escaping parameter 'callback'
處理:使用@escaping修飾閉包參數(shù)
參考:
https://blog.csdn.net/Milan__Kundera/article/details/104836512
Initializer for conditional binding must have Optional type, not 'HomeBannerModel'
處理:注意if和guard判斷的變量都需要是optional類型智润,即可選類型
- 參考:
https://blog.csdn.net/weixin_30362083/article/details/97834167
http://www.reibang.com/p/406ebbfc0dfc
Expected parameter name followed by ':'
- 處理:這個和閉包的參數(shù)設(shè)置有關(guān)系
'nil' is not compatible with expected argument type 'HomeRecommendModel'
weak unowned的區(qū)別
Unowned 引用,像weak引用一樣未辆,不會增加對象的引用計數(shù)窟绷。
在引用對象的生命周期內(nèi),如果它可能為nil咐柜,那么就用weak引用兼蜈。反之,當你知道引用對象在初始化后永遠都不會為nil就用unowned拙友。
swift的派發(fā)機制:
- swift的派發(fā)機制:
- 函數(shù)的派發(fā)機制:靜態(tài)派發(fā)(直接派發(fā))为狸、函數(shù)表派發(fā)、消息派發(fā)
- swift派發(fā)機制總結(jié):
● swift中所有值類型:struct遗契、enum使用直接派發(fā)
● swift中協(xié)議的extensions(類似于OC的分類)使用直接派發(fā)辐棒,初始聲明函數(shù)使用函數(shù)表派發(fā)
● swift中class的extensions使用直接派發(fā),初始化聲明函數(shù)使用函數(shù)表派發(fā)牍蜂,dynamic修飾的函數(shù)使用消息派發(fā)
● swift中NSObject的子類用@nonobjc或final修飾的函數(shù)使用直接派發(fā)漾根,初始聲明函數(shù)使用函數(shù)表派發(fā),dynamic修飾的extensions使用消息派發(fā) - swift中函數(shù)派發(fā)查看方式:可將swift代碼轉(zhuǎn)換為SIL(中間碼)
swiftc -emit-silgen -O example.swift