swift4.0 適配

原文地址:swift4.0 適配


一、前言

在我們的工程中處于swift和OC混編的狀態,使用swift已經有一年半的時間了,隨著Xcode9的更新,swift3.2和swift4.0也隨之到來,swift3.2相較于Xcode8的swift3.1變動極小,適配沒遇到問題,主要關注swift4.0的適配。

二、查看當前工程的 swift 版本

三、使用 Xcode 將工程轉換到 swift4.0

1、環境

Xcode9.1

當前swift版本 3.2

2、轉換步驟:

選中要轉換的target

Edit -> Convert -> To Current Swift Syntax

勾選需要轉換的target(pod引用不用勾選),Next

選擇轉換選項,Next

這兩個選項是關于swift的@objc推斷特性的,如果使用了swift4.0顯式的@objc屬性,能減少整體代碼的大小。此時我們選 Minimize Inference(recommend),

關于兩個選項:

Minimize Inference(recommend)

根據靜態推斷,僅在需要的地方添加@objc屬性。使用此選項后,需要按照Completing a Swift 4 minimize inference migration來完成轉換。

Match Swift 3 Behavior

在編譯器隱式推斷的任何地方向代碼添加一個@objc屬性。這個選項不會改變你的二進制文件的大小,因為被Swift 3隱式推斷在所有的地方都添加了顯式的@objc屬性。

預覽轉換代碼,沒問題,Save。

3、修改錯誤

完成上述5步之后,看一下swift版本,已經是4.0了:

至此打完收工,適配結束。然而并沒有,當你運行的時候會看到這個:

是否欲哭無淚,居然這么多錯誤,不用怕,其實要改動的地方并不多,有些都是重復的,可以直接全局替換就行。

舉個栗子:

- class dynamic func

// 轉換前
class dynamic func bookMoneyToUpController() -> MPBookMoneyToUpController {
??? let vc = MPBookMoneyToUpController.init(nibName: "MPBookMoneyToUpController", bundle: Bundle.main)
??? return vc
}

// 轉換后
class @objc dynamic func bookMoneyToUpController() -> MPBookMoneyToUpController {
??? let vc = MPBookMoneyToUpController.init(nibName: "MPBookMoneyToUpController", bundle: Bundle.main)
??? return vc
}

// 問題 @objc 修飾符需要前置
// 修改成下面即可
@objc class dynamic func bookMoneyToUpController() -> MPBookMoneyToUpController {
??? let vc = MPBookMoneyToUpController.init(nibName: "MPBookMoneyToUpController", bundle: Bundle.main)
??? return vc
}

// 全局替換即可
class @objc dynamic func? -> @objc class dynamic func

上面使用dynamic修飾符是由于以前使用JSPatch來做hotfix,需要用到原來OC的運行時特性。

四、@objc

swift4.0最大的特性之一就是@objc修飾符的變化了,它主要處理OC和swift混編時一些方法的調用以及屬性獲取問題,swift4.0將在swift3.x中一些隱式類型推斷的特性去除以后,需要我們來手動管理@objc修飾符。

在上文中使用Xcode轉換swift4.0時我們勾選了Minimize Inference選項,那么我們就需要手動處理相關的@objc修飾符,來保證OC和swift代碼能正常相互調用。

1、@objc修飾符手動處理步驟

使用“最小化”轉換代碼后,需要處理構建和運行時的問題,在完成初始的swift4.0轉換后,需要按照下面步驟來處理其它問題。

1. 運行你的工程

2. 修復編譯器提示需要添加@objc的地方

3. 測試你的代碼,并修復編譯器提示使用了不推薦的隱式@objc引用的警告。直到沒有警告發生。

打開工程的build settings.

將Swift 3 @objc inference設置為Default.

2、@objc修飾符需要處理的問題

編譯警告

swift 中編譯的警告

#selector參數指定的實例方法必須使用@objc修飾,因為swift4中棄用了@objc屬性推斷。

// 下面的代碼會有警告
class MyClass : NSObject {
??? func foo() {
??? }
???
??? func bar() {
??????? self.perform(#selector(MyClass.foo)
??? }
}
warning: argument of ‘#selector’ refers to instance method ‘foo’ in ‘MyClass’ that depends

Objective-C 編譯時警告

在OC中調用的swift方法,在swift中需要追加@objc修飾,swift4廢棄了該類型推斷。

// 下面的代碼會有警告
@implementation MyClass (ObjCMethods)
- (void)other {
??? [self foo];
}
@end
warning: Swift method MyClass.foo uses @objc inference deprecated in Swift 4; add @objc to provide an Objective-C entrypoint

修復編譯時警告

// 通過追加 @objc 來消除警告
class MyClass : NSObject {
??? @objc func foo() {
??? }
???
??? func bar() {
??????? self.perform(#selector(MyClass.foo)
??? }
}


查看所有需要添加@objc的編譯警告

直接選中定位到相應位置,追加@objc修飾即可。

運行時警告

運行時警告會打印在控制臺:

***Swift runtime:
ClassName.swift:lineInFile:columnInLine:
entrypoint -[ClassName methodName] generated by implicit @objc inference is deprecated and will be removed in Swift 4;
add explicit @objc to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message

在Xcode9.1中,運行時警告在這里也能看到:

想要修復運行時警告,需要添加@objc修飾符到對應的方法或者符號。

運行時警告的常見原因:

在OC中使用SEL

在swift中使用了perform methods

在OC中使用了performSelector methods

使用了@IBOutlet或者@IBAction

class MyClass : NSObject {
??? func foo() {
??? }
???
??? func bar() {
??????? let selectorName = "foo"
??????? self.perform(Selector(selectorName)
??? }
}
***Swift runtime: MyClass.swift:7:7: entrypoint -[MyClass foo] generated by implicit @objc inference is deprecated and will be removed in Swift 4; add explicit @objc to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message

五、swift4.0其它部分特性

1、NSAttributedStringKey

NSAttributedString的初始化方法變化:

// swift3.x
public init(string str: String, attributes attrs: [AnyHashable : Any]? = nil)

// swift4.0
public init(string str: String, attributes attrs: [NSAttributedStringKey : Any]? = nil)

示例:

// 轉換前
let attributes = [NSForegroundColorAttributeName: RGB(128, g: 134, b: 146),
????????????????? NSParagraphStyleAttributeName: paragraph,
????????????????? NSFontAttributeName: UIFont.systemFont(ofSize: 14)] as [String : Any]
var tipAttrText = NSAttributedString.init(string: tipText, attributes: attributes)

// 轉換后
let attributes = [NSAttributedStringKey.foregroundColor.rawValue: RGB(128, g: 134, b: 146),
????????????????? NSAttributedStringKey.paragraphStyle: paragraph,
????????????????? NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)] as! [String : Any]
var tipAttrText = NSAttributedString(string: tipText, attributes: attributes)

// tipAttrText 初始化報錯提示
Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]?'

// 修改
NSAttributedStringKey.foregroundColor.rawValue -> NSAttributedStringKey.foregroundColor
去掉 as! [String : Any]

2、String

String的characters屬性被廢棄了

let string = "abc" var count = string.characters.count // 第二行報錯 'characters' is deprecated: Please use String or Substring directly // 對應新方法 count = string.count

String的addingPercentEscapes方法被廢棄了

// swift3.x
var url = @"http://www.example.com?username=姓名"
url = url.addingPercentEscapes(using: String.Encoding.utf8)!

// 報錯
'addingPercentEscapes(using:)' is unavailable: Use addingPercentEncoding(withAllowedCharacters:) instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.

// 修改
uri = uri.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!

substring(to:)被廢棄了

let index = tagText.index(tagText.startIndex, offsetBy: MPMultipleStyleListItemTagMaxLength)

// 警告:'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator.
let b = tagText.substring(to: index)

// 新 API
// 注意:a 的類型是 Substring,不是 String
let a = tagText.prefix(upTo: index)

3、initialize 廢棄

// swift3.x
override class func initialize() {
??? // some code
}

// 報錯
Method 'initialize()' defines Objective-C class method 'initialize', which is not permitted by Swift

Swift3.x 繼續 Method Swizzling這篇文章里面介紹了一種解決思路。

4、swift3使用#selector指定的方法,只有當方法權限為private時需要加@objc修飾符,swift4.0都要加@objc修飾符

// 示例代碼

func startMonitor() {

NotificationCenter.default.addObserver(self, selector: #selector(self.refreshUserLoginStatus), name: NSNotification.Name.XSLUserLogin, object: nil)

}

func refreshUserLoginStatus() {

// some code

}

// 第二行警告

Argument of '#selector' refers to instance method 'refreshUserLoginStatus()' in 'MPUnreadMessageCountManager' that depends on '@objc' inference deprecated in Swift 4

// 追加 private

func startMonitor() {

NotificationCenter.default.addObserver(self, selector: #selector(self.refreshUserLoginStatus), name: NSNotification.Name.XSLUserLogin, object: nil)

}

private func refreshUserLoginStatus() {

// some code

}

// 第二行報錯

Argument of '#selector' refers to instance method 'refreshUserLoginStatus()' that is not exposed to Objective-C

swift4.0不再允許重載extension中的方法(包括instance、static、class方法)

// 示例代碼
class TestSuperClass: NSObject {
}
extension TestSuperClass {
??? func test() {
??????? // some code
??? }
}
class TestClass: TestSuperClass {
??? // 報錯:Declarations from extensions cannot be overridden yet
??? override func test() {
??????? // some code
??? }
}

六、pod引用

添加以下內容到Podfile。

post_install do |installer|
installer.pods_project.targets.each do |target|
if ['WTCarouselFlowLayout', 'XSLRevenue', 'OHHTTPStubs/Swift'].include? target.name
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '3.2'
end
end
end
end

七、踩坑

UITableViewDelegate協議方法名變更,沒有錯誤提示:

// swift3.x
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat

// swift4.0
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、前言 在我們的工程中處于swift和OC混編的狀態,使用swift已經有一年半的時間了,隨著Xcode9的更新...
    wentianen閱讀 2,720評論 0 4
  • 1、隨機數 不需要隨機數種子 arc4random()%N + begin:產生begin~begin+N的隨機數...
    我是小胡胡123閱讀 4,239評論 0 2
  • iOS開發中,我們需要根據用戶需要去適配各種各樣的版本,特別是蘋果爸爸的每一次新版本發布,作為開發者的我們永遠是最...
    青蘋果園閱讀 7,615評論 2 12
  • 只為個人記錄下來看看 swift4.0 適配 原文地址:http://blog.csdn.net/andanlan...
    擁抱月亮的大星星閱讀 385評論 0 1
  • 老教授要帶領我們參觀圖書館。因為白天人太多,所以選在晚上過去。 我們大約是一個班的人數,由教授和班長領路。班長好像...
    愿河閱讀 256評論 0 1