swift使用相关技术要点

swift项目工程搭建

  • swift教程:
    https://www.runoob.com/swift/swift-tutorial.html

  • 参考:
    https://www.jianshu.com/p/f324c2e95b34
    https://blog.csdn.net/qq_33298465/article/details/74451765

swift常用框架

  • 参考:
    https://www.jianshu.com/p/fbe0806549b6
    https://blog.csdn.net/zWbKingGo/article/details/109514983
    https://blog.csdn.net/zWbKingGo/article/details/109514983

SnapKit

  • 自动布局三方库,类似于OC的Masonry。

  • 参考:
    https://www.jianshu.com/p/2bad53a2a180

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的部分,设置版本为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.
  • 处理:需要在手机设置-通用-设备管理:信任开发者账号

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

  • 参考:
    https://www.jianshu.com/p/52ec78e1229a
    https://www.jianshu.com/p/d1380241c4cf
    https://jingyan.baidu.com/article/ea24bc39e64a90da62b3313b.html

swift UITabBarController再push一个页面会被tabbar遮挡

  • 设置hidesBottomBarWhenPushed为YES即可。
  • 参考:
    https://blog.csdn.net/u011452278/article/details/52858910

swift问题

Initializer for conditional binding must have Optional type, not 'MineModelData'
  • 参考:
    https://www.jianshu.com/p/406ebbfc0dfc
    https://www.cnblogs.com/pengsi/p/6669547.html
Property 'self.label' not initialized at super.init call
  • 参考:
    https://blog.csdn.net/Morris_/article/details/121286617
Cannot find 'MineReuseIdentifier' in scope
  • 参考:
    https://blog.csdn.net/Morris_/article/details/107382069
Value of type 'UITableViewCell' has no member 'updateCell'
  • 原因:UITableViewCell使用不对

Alamofire

  • 网络请求三方库,类似于OC的AFNetwork

  • 参考:
    https://www.jianshu.com/p/b7174ed30901
    http://www.zzvips.com/article/129480.html

swift extension

  • 参考:
    https://www.jianshu.com/p/783df05a9b59
  • extension:与OC分类/扩展区别
    参考:
    https://www.jianshu.com/p/120d3d3446dd

swift class/struct

  • struct和class区别

    • 相同点:
      1.定义属性用于存储值 (property)
      2.定义方法用于提供功能 (function)
      3.定义下标操作使得可以通过下标语法来访问实例所包含的值 (subscript)
      4.定义构造器用于生成初始化值 (initializers)
      5.通过扩展以增加默认实现的功能 (extension)
      6.实现协议以提供某种标准功能 (protocol)

    • 不同点:
      1.class是引用类型;struct是值类型
      2.class支持继承;struct不支持继承
      3.class声明的方法修改属性时不需要mutating关键字;struct需要
      4.class中每一个成员变量都必须被初始化,否则编译器会报错,而struct不需要,编译器会自动帮我们生成init函数,给变量赋一个默认值
      5.class支持引用计数(reference counting)(允许对一个类的多次引用),struct不支持
      6.class支持type casting(类型转换)(允许在运行时检查和解释一个类实例的类型),struct不支持
      7.class支持deinitializers(析构器)(允许一个类实例释放任何其所被分配的资源),struct不支持
      8.变量赋值方式不同(深浅copy),class浅拷贝,struct深拷贝,class的赋值是传递引用,struct则是copy传值,不是使用引用计数。
      9.内存管理:struct存储在stack中,class存储在heap中、
      10.方法派发方式:struct的方法调用是静态绑定,而class的方法调用是动态实现

  • 参考:
    https://www.cnblogs.com/beckwang0912/p/8508299.html

swift public/private/open

  • swift public/private/open/internal/fileprivate
    Swift文件权限:open/public/internal/private/fileprivate区别,默认权限internal
    1)private 访问级别所修饰的属性或者方法只能在当前类里访问。
    2)fileprivate 访问级别所修饰的属性或者方法在当前的 Swift 源文件里可以访问。
    3)internal 访问级别所修饰的属性或方法在源代码所在的整个模块都可以访问。
    如果是框架或者库代码,则在整个框架内部都可以访问,框架由外部代码所引用时,则不可以访问。
    如果是 App 代码,也是在整个 App 代码,也是在整个 App 内部可以访问。
    4)public可以被任何人访问。但其他 module 中不可以被 override 和继承,而在 module 内可以被 override 和继承。
    5)open可以被任何人使用,包括 override 和继承。

  • 参考:
    https://blog.csdn.net/chenyong05314/article/details/116267430

getTopViewController

  • 参考:
    https://blog.csdn.net/zgpeace/article/details/108237716
    https://www.jianshu.com/p/804d603085b2

swift宏定义

  • 参考:
    https://blog.csdn.net/qq_25639809/article/details/60959286

swift @available / #available

  • 参考:
    https://www.jianshu.com/p/eef6c563a1ea
    https://blog.haohtml.com/archives/16972

  • 注意:
    用于语句:#available
    用于函数/类/协议:@available

swift: Type of expression is ambiguous without more context

  • 参考:
    https://www.jianshu.com/p/93ad2a9af85d
UIApplication.shared.windows.statusBarManager?.statusBarFrame.size.height
  • 参考:
    https://www.jianshu.com/p/314991288ff8

swift: cast error

Cast from 'AnyClass?' (aka 'Optional') to unrelated type 'ViewController' always fails
  • 参考:
    https://www.jianshu.com/p/cb421b03418f

swift: compiler error

error: Segmentation fault: 11 (in target 'app-demo-ios' from project 'app-demo-ios)
  • 说明:之前有对Legacy Build System进行配置的方案。但是目前该方案已经不适用了。

  • 参考:
    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设置没有关系。
    是和变量定义和if let等判断错误有关。

  • 参考:
    https://zhuanlan.zhihu.com/p/245086610

swift No exact matches in call to initializer

  • 参考:
    https://www.jianshu.com/p/f8dff4768a4b

Initializer for conditional binding must have Optional type, not 'ViewController'

  • 参考:
    https://www.jianshu.com/p/406ebbfc0dfc
    https://blog.csdn.net/qq_15623599/article/details/88969525

swift as:swift强制类型转换

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

  • 懒加载
  • 参考:
    https://blog.csdn.net/LiqunZhang/article/details/115069544

swift button

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSLayoutConstraint for >: 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/97128540

  • swift AppDelegate添加view不显示
    参考:
    https://www.jianshu.com/p/984b56f09261
    https://www.jianshu.com/p/a14550af3e89

  • 最终处理方案:

[[UIApplication sharedApplication].keyWindow addSubview:label];

swift dispatch

  • 参考:
    https://www.jianshu.com/p/59136f0241bb

swift/oc

  • swift与oc区别
    1.swift是静态语言,有类型推断,OC是动态语言。
    2.swift面向协议编程,OC面向对象编程
    3.swift注重值类型,OC注重引用类型。
    4.swift支持泛型,OC只支持轻量泛型
    5.swift支持静态派发(效率高)、动态派发(函数表派发、消息派发)方式,OC支持动态派发(消息派发)方式。
    6.swift支持函数式编程
    7.swift的协议不仅可以被类实现,也可以被struct和enum实现
    8.swift有元组类型、支持运算符重载
    9.swift支持命名空间
    10.swift支持默认参数
    11.swift比oc代码更加简洁

  • 参考:
    https://www.jianshu.com/p/3b7f3f596bcb

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:响应日志

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

  • 参考:
    https://blog.csdn.net/Morris_/article/details/116705848

Generic parameter 'T' could not be inferred

无法推断泛型参数'T'

网络连接:日志

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)"}}))

  • 原因:应该是没有在info.plist中申请网络权限

  • 处理:
    App Transport Security Settings
    设置Allow Arbitrary Loads为YES

import Kingfisher

  • 图片处理库

  • 参考:
    https://www.jianshu.com/p/fc91ddbb5cbb

crypto导入swift

  • 参考:
    https://wenku.baidu.com/view/6842a06b2d3f5727a5e9856a561252d380eb20cd.html
    https://www.sohu.com/a/417205679_99956743
    https://www.csdn.net/tags/MtTaEg3sMDYwMzQ1LWJsb2cO0O0O.html

  • swift crypto
    参考:
    https://codingdict.com/os/software/50757
    https://blog.csdn.net/chechengxue/article/details/109308418

  • CryptoSwift介绍
    参考:
    https://github.com/krzyzanowskim/CryptoSwift

  • swiftCrypto使用
    参考:
    https://www.hangge.com/blog/cache/detail_1865.html

  • github:
    https://github.com/krzyzanowskim/CryptoSwift

swift Digest

  • 参考:
    https://www.jianshu.com/p/879dee10636b

Xcode remove Package Dependencies

  • 参考:
    https://blog.csdn.net/iCloudEnd/article/details/108233951

swift WKWebView

  • 参考:
    https://blog.csdn.net/sinat_31177681/article/details/107099845

swift轮播图

  • 参考:
    https://blog.csdn.net/mp624183768/article/details/108601692
    https://www.jianshu.com/p/776d1333717a
    https://www.jianshu.com/p/9b72dd21d222

PageMenuView:swift可以来回切的有tab的view

  • 参考:
    https://wenku.baidu.com/view/cab9d4fb5322aaea998fcc22bcd126fff7055d61.html
    https://www.jianshu.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'
  • 处理:
    需要调用UIViewController的初始化方法

  • 参考:
    https://blog.csdn.net/Morris_/article/details/121283519

定义属性时 初始化错误

Property 'self.widthConstraint' not initialized at super.init call
  • 处理:有两种方式
    一是:在调用super.init之前设置self.widthConstraint
    二是如下定义:
    fileprivate var widthConstraint: NSLayoutConstraint!
    注意:第二种方式最后有一个感叹号,如果没有就会报上面的错误

  • 参考:
    https://www.jianshu.com/p/bbd7774550d2

swift布局:错误

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors  and  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)

  • 参考:
    https://www.jianshu.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
    https://www.jianshu.com/p/e85c6e12a539
    https://www.jianshu.com/p/293c16132d0a
    https://www.jianshu.com/p/5fe6330ee84a
    http://t.zoukankan.com/metaphors-p-9405432.html

swift DataResponse

  • 参考:
    https://www.jianshu.com/p/0985b87ae812
response.value:Optional(<__NSArrayI 0x281146730>(
  • 参考:
    https://www.csdn.net/tags/NtjaEg3sOTI5NDMtYmxvZwO0O0OO0O0O.html

escaping

Escaping closure captures non-escaping parameter 'callback'
  • 处理:使用@escaping修饰闭包参数

  • 参考:
    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
    https://www.jianshu.com/p/406ebbfc0dfc
Expected parameter name followed by ':'
  • 处理:这个和闭包的参数设置有关系
'nil' is not compatible with expected argument type 'HomeRecommendModel'
  • 参考:
    https://blog.csdn.net/weichuang_1/article/details/50363389

weak unowned的区别

  • Unowned 引用,像weak引用一样,不会增加对象的引用计数。

  • 在引用对象的生命周期内,如果它可能为nil,那么就用weak引用。反之,当你知道引用对象在初始化后永远都不会为nil就用unowned。

  • 参考:
    https://www.jianshu.com/p/bb321f256b58

swift的派发机制:

  • swift的派发机制:
  1. 函数的派发机制:静态派发(直接派发)、函数表派发、消息派发
  2. swift派发机制总结:
    ● swift中所有值类型:struct、enum使用直接派发
    ● swift中协议的extensions(类似于OC的分类)使用直接派发,初始声明函数使用函数表派发
    ● swift中class的extensions使用直接派发,初始化声明函数使用函数表派发,dynamic修饰的函数使用消息派发
    ● swift中NSObject的子类用@nonobjc或final修饰的函数使用直接派发,初始声明函数使用函数表派发,dynamic修饰的extensions使用消息派发
  3. swift中函数派发查看方式:可将swift代码转换为SIL(中间码)
    swiftc -emit-silgen -O example.swift
  • 参考:
    https://www.jianshu.com/p/ec7c9f21565b

你可能感兴趣的:(swift使用相关技术要点)