iOSSwift

Swift3.2升级4.0

2017-12-23  本文已影响662人  fordring2008

一, Swift 版本

1, Swift Language ---> 4.0

2, 修改pod库

第三方库暂时使用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,Swfit4.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)

三,其他报错

1, Closure cannot implicitly capture a mutating self parameter"错误
在struct中,如果我们在闭包中使用self,就会得到此错误
比如:

struct RecordModel {
    /// 定义一个闭包
    var action: (() -> ())?
    var height = 10
    
    self.action = { 
        self.height = 20 
        //Closure cannot implicitly capture a mutating self parameter报错
    }
}

解决方案1:
为closure增加一个inout类型的参数

struct RecordModel {
    /// 定义一个闭包
    var action: ((_ inSelf: inout RecordModel) -> ())?
    var height = 10
    
    self.action = { (inSelf) in
        inSelf.height = 20 
    }
}

根据inout类型的说明,我们知道,实际上这相当于增加了一个隐藏的临时变量,self被复制,然后在closure(闭包)中使用,完成后,再复制回self。也就是说,这个方法有额外的内存开销。如果是struct较大的情形,这么做并不划算

解决方案1:使用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变量之间的关系:

2, Declarations from extensions cannot be overridden yet 错误
这个错误大致是因为,协议方法是在extension里面的,不能被重写
就是在每一个需要此协议的类里面,重新遵循代理,实现该协议方法

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() {//此处报错
    }
}

替代方法:

extension UIViewController {
    public override class func initializeOnceMethod() {
    }
}

//在AppDelegate的方法中调用:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
    UIViewController.initializeOnceMethod()
}

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

另外--->

// NSTextCheckingResult
func rangeAt(_: Int)   --> func range(at: Int)

String的警告
'characters' is deprecated: Please use String or Substring
characters 过期

let string = "abc"
var count = string.characters.count

// 第二行报错
'characters' is deprecated: Please use String or Substring directly

// 对应新方法
count = string.count

warning
Conditional downcast from 'NSData?' to 'Data' is a bridging conversion; did you mean to use 'as'?

// data = NSData(contentsOfFile: path!) as? Data
   data = NSData(contentsOfFile: path!) as Data?

waring
'M_PI' is deprecated: Please use 'Double.pi' or '.pi' to get the value of correct type and avoid

M_PI  --->  Double.pi
上一篇下一篇

猜你喜欢

热点阅读