SWIFT, 苹果于 2014 年 WWDC(苹果开发者大会)发布的新开发语言, 可与 Objective-C * 共同运行于 Mac OS 和 iOS 平台, 用于搭建基于苹果平台的应用程序
这篇文章主要给大家介绍了关于升级到 Swift 4.0 可能遇到的坑的相关资料, 文中通过示例代码介绍的非常详细, 对大家学习或者使用 swift4 具有一定的参考学习价值, 需要的朋友们下面随着小编来一起学习学习吧
前言
swift4.0 已经出来一段时间, 之前已经给大家总结介绍了关于 swift4 的新特性, 那么本文就来介绍下当 swift 升级到 swift4 在使用中会遇到哪些问题呢? 下面话不多说了, 来一起看看详细的介绍吧
升级 Swift4.0
并不是所有库都能做到及时支持 Swift4.0, 更何况是在现在连 Xcode9 也还是 beta 的状态
所以我们仅能做到将自己的业务代码 (主工程代码) 部分升级到 Swift4.0, 然后同时保留各种 pod 库在 Swift3.2 版本
没办法, 谁叫 Swift4.0 也还无法做到 API 兼容呢(但愿能在 Swift5 之前实现吧)
至于我说的同时使用两个版本的 Swift, 这是没问题的, Xcode9 支持在项目中同时使用 Swift3.2 和 Swift4.0
一. 修改 Swift 版本
1. 如下图指定主工程的 Swift 版本为 4.0
2. 修改 pod 库
在 Podfile 文件的最下方加入如下代码, 指定 pod 库的 Swift 版本为 3.2(这样会使得所有的第三方 pod 库的 Swift 版本都为 3.2)
- post_install do |installer|
- installer.pods_project.targets.each do |target|
- target.build_configurations.each do |config|
- config.build_settings['SWIFT_VERSION'] = '3.2'
- end
- end
- end
二. 主工程中的代码修改
1. 列举一下 Swift3.2 到 Swift4.0 的改变(只是我项目中遇到的):
1). Swift4.0 中对于扩展的属性(包括实例属性 static 属性 class 属性), 都只能使用 get 方法, 不可使用 set 方法
2). Swift4.0 中不再允许复写扩展中的方法(包括实例方法 static 方法 class 方法)
比如: 自定义的协议方法在 extension 中实现, 若某个类遵循了该协议, 其子类便不能重写该协议方法
解决的方法是: 在每个需要该协议的类里面都重新遵循该协议, 实现协议方法
个人想到的办法, 不知道有没有其他解决办法可以提供一下
3). swift3 使用 #selector 指定的方法, 只有当方法权限为 private 时需要加 @objc 修饰符, 现在 Swift4.0 全都要加 @objc 修饰符
4). 自定义的 protocol 协议中, 有 optional 修饰的非必须实现的方法, 需要用 @objc 修饰
5). 字体方面的一些重命名
- NSFontAttributeName---.font
- // 或者 NSAttributedStringKey.font
- NSForegroundColorAttributeName---.foregroundColor
- //NSAttributedStringKey.foregroundColor
- NSStrikethroughStyleAttributeName---.strikethroughStyle
- //NSAttributedStringKey.strikethroughStyle
- // 字符串类型的, 添加 rawValue
- NSAttributedStringKey.font.rawValue
- // 等等等等..........
- // 大部分类似以下, 涉及富文本的方法均已改为了 NSAttributedStringKey 类型
- addAttributes(_ attrs: [NSAttributedStringKey: Any] = [: ], range: NSRange)
三. 项目中遇到的一些的报错问题
3-1. "Closure cannot implicitly capture a mutating self parameter" 错误
在 struct 中, 如果我们在闭包中使用 self, 就会得到 Closure cannot implicitly capture a mutating self parameter 的错误提示比如:
- struct RecordModel {
- /// 定义一个闭包
- var action: (() -> ())?
- var height = 10
- self.action = {
- self.height = 20
- //Closure cannot implicitly capture a mutating self parameter 报错
- }
- }
++ 并且由于 RecordModel 的类型是 struct, 我们也没发在 action 闭包里添加截获列表那么是不是就必须使用 class 了? 答案是否定的有两种方式可以解决这个问题 ++
方案一: 为 closure 增加一个 inout 类型的参数
- struct RecordModel {
- /// 定义一个闭包
- var action: ((_ inSelf: inout RecordModel) -> ())?
- var height = 10
- self.action = { (inSelf) in
- inSelf.height = 20
- }
- }
根据 inout 类型的说明, 我们知道, 实际上这相当于增加了一个隐藏的临时变量, self 被复制, 然后在 closure(闭包)中使用, 完成后, 再复制回 self 也就是说, 这个方法有额外的内存开销如果是 struct 较大的情形, 这么做并不划算
方案二: 使用 UnsafeMutablePointer<Pointee>
== 这次采用直接指针的方式对于 struct 来进行操作, 采用指针的好处是 self 不会被多次复制, 性能较高缺点是你需要自行确定你的代码的安全 ==
- struct RecordModel {
- /// 定义一个闭包
- var action: (() -> ())?
- var height = 10
- let selfPointer = UnsafeMutablePointer(&self)
- self.action = {
- selfPointer.pointee.height = 20
- }
- }
结论
==Closure cannot implicitly capture a mutating self parameter 错误的原因是在进出 closure(闭包)之后, self 的一致性没办法得到保证, 所以编译器默认不允许在 struct 的 closure(闭包)中使用 self 如果我们确定这么做是安全的, 就可以通过上面的两种方式解决这个问题其中, 方法二的性能更好一些 ==
注意
这里可以记一下指针和 swift 变量之间的关系:
UnsafePointer 对应 let
UnsafeMutablePointer 对应 var
AutoreleasingUnsafeMutablePointer 对应 unowned UnsafeMutablePointer, 用于 inout 的参数类型
UnsafeRawPointer 对应 let Any,raw 系列都是对应相应的 Any 类型
UnsafeBufferPointer 是 non-owning 的类型(unowned), 用于 collection 的 elements, buffer 系列均如此
3-2. Declarations from extensions cannot be overridden yet 错误
== 这个错误大致是因为, 协议方法是在 extension 里面的, 不能被重写 ==
解决办法:(仅供参考, 如有更好的建议还望多多指教)
小编想到的解决办法就是在每一个需要此协议的类里面, 重新遵循代理, 实现该协议方法
3-3. "Method'initialize()'defines Objective-C class method'initialize', which is not permitted by Swift"
== 报错原因: 在于已经废弃的 initialize 方法, 示例如下 ==
方法交叉(Method Swizzling)
有时为了方便, 也有可能是解决某些框架内的 bug, 或者别无他法时, 需要修改一个已经存在类的方法的行为方法交叉可以让你交换两个方法的实现, 相当于是用你写的方法来重载原有方法, 并且还能够是原有方法的行为保持不变
- extension UIViewController {
- public override class func initialize() { // 此处报错
- // 此处省略 100 行代码
- }
- }
initialize 该方法已经被 Swift4.0 废弃
在 Swift3.0 还勉强可以使用, 但是会有警告; 但是在 4.0 已经被完全废弃
== 替代方法:==
在 app delegate 中实现方法交叉
像上面通过类扩展进行方法交叉, 而是简单地在 app delegate 的 application(_:didFinishLaunchingWithOptions:) 方法调用时调用该方法
- extension UIViewController {
- public override class func initializeOnceMethod() {
- // 此处省略 100 行代码
- }
- }
- // 在 AppDelegate 的方法中调用:
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ? =nil) - >Bool {
- // 此处省略 100 行代码
- UIViewController.initializeOnceMethod()
- }
- 3 - 4.'dispatch_once'is unavailable in Swift: Use lazily initialized globals instead
报错原因: dispatch_once 在 Swift4.0 也已经被废弃
- extension UITableView {
- struct once{
- static var onceTaken:Int = 0
- }
- dispatch_once(&once.onceTaken) { () -> Void in
- // 在这里 dispatch_once 就会报错
- // 此处省略 1000000 行代码
- }
- }
解决方法: 通过给 DispatchQueue 添加扩展实现
- extension DispatchQueue {
- private static var _onceTracker = [String]()
- public class func once(token: String, block: () -> ()) {
- objc_sync_enter(self)
- defer {
- objc_sync_exit(self)
- }
- if _onceTracker.contains(token) {
- return
- }
- _onceTracker.append(token)
- block()
- }
- func async(block: @escaping ()->()) {
- self.async(execute: block)
- }
- func after(time: DispatchTime, block: @escaping ()->()) {
- self.asyncAfter(deadline: time, execute: block)
- }
- }
使用字符串 token 作为 once 的 ID, 执行 once 的时候加了一个锁, 避免多线程下的 token 判断不准确的问题
使用的时候可以传 token
- DispatchQueue.once(token: "tableViewOnce") {
- print("Do This Once!")
- }
或者使用 UUID 也可以:
- private let _onceToken = NSUUID().uuidString
- DispatchQueue.once(token: _onceToken) {
- print( "Do This Once!" )
- }
四 swift3.2 升级到 swift4.0 扫码不走回调方法
xcode 升级到 9.0 swift 改到 swift4.0 之后扫码一直不走回调 , 研究了好长时间, 发现苹果把扫码的代理方法的参数变了之前的方法
- func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)
- func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!)
这是之前 swift3.2 的代理方法, swift4.0 之后不会走这两个代理方法, 原因是现在代理方法不一样了
- func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)
- func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection)
swift4.0 的两个代理方法对比之前的 3.2 方法, 可以发现现在方法的参数变了
将之前的两个方法的参数改好, 扫码就可以正常用了
来源: http://www.phperz.com/article/18/0217/361471.html