swift使用相关技术要点

swift项目工程搭建

swift常用框架

SnapKit

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'

swift UITabBarController

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

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

swift extension

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

swift宏定义

swift @available / #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)
: 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.

swift No exact matches in call to initializer

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

swift as:swift强制类型转换

Cannot convert value of type 'SettingsViewController' to type 'ViewController' in coercion

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

swift ==和===

UIWindow:swift添加到UIWindow

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

swift dispatch

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

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

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

crypto导入swift

swift Digest

Xcode remove Package Dependencies

swift WKWebView

swift轮播图

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

UIViewController 初始化错误

Must call a designated initializer of the superclass 'UIViewController'

定义属性时 初始化错误

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 <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.'

SwiftyJSON

git

swift DataResponse

response.value:Optional(<__NSArrayI 0x281146730>(

escaping

Escaping closure captures non-escaping parameter 'callback'
Initializer for conditional binding must have Optional type, not 'HomeBannerModel'

处理:注意if和guard判断的变量都需要是optional类型,即可选类型

Expected parameter name followed by ':'
  • 处理:这个和闭包的参数设置有关系
'nil' is not compatible with expected argument type 'HomeRecommendModel'

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容