1. 程式人生 > >iOS 11系列 - Xcode 9新特性

iOS 11系列 - Xcode 9新特性

rsa cti 連接數 項目 threads 比較 title and val

Xcode 9最近剛剛發布,帶來了一系列不錯的新特性,可以更好的幫助到開發者完成開發工作。

Xcode Runtime Tool

Xcode 9中有許多Runtime Tool可以幫助開發者找到代碼錯誤,包括如下:

  • Main Thread Checker - Xcode 9新引入

  • Address Sanitizer

  • Thread Sanitizer

  • Undefined Behavior Sanitizer

  • Using Runtime Tools Effectively

Main Thread Checker

Main Thread Checker可以幫助開發者找到不在主線程中執行的UI操作。

技術分享

設置可以在Diagnostics面板中找到,Xcode默認勾上:

技術分享

在運行時刻,如果發現有任何的Main Thread問題,則會提示如下:

技術分享

Address Sanitizer

Address Sanitizer可以用於檢測內存問題,Address Sanitizer開啟會帶來比較大的Overhead,所以需要開發者手動設置。

技術分享

開啟之後一旦發現有任何內存問題,就會自動檢測並且提示,如下圖所示:

1. 會有提示告知哪行代碼有使用已經釋放的內存對象

2. 左邊面板會告知該對象具體的創建、使用、釋放的情況,非常方便debug

技術分享

Thread Sanitizer

用於發現多線程問題,在Xcode 9中的Thread Sanitizer可以幫助:

1. 發現多線程中的數據競賽問題

2. 在集合中的數據競賽

3. Swift access races

如下圖所示就是Swift關於Array的數據多線程競賽問題:

技術分享

可以通過引入Queue來同步多個線程的方式來解決:

技術分享

通常解決多線程數據競賽問題的方法:

1. 使用GCD同步數據操作

2. 使用Serial Queue將共享數據的操作串型化

3. Thred Sanitizer是發現數據競賽的很好的工具

Undefined Behaviour Sanitizer

顧名思義,Undefined Behaviour Sanitizer可以幫助開發者在運行時找到一些異常情況,包括如下情況:

1. 運行時的Bug查找:整型溢出,

2. 檢查C中的不安全的Constructs

3. 和其他運行時的工具可以兼容

Using Runtime Tools Effectively

Apple對於Xcode 9中的Runtime工具提供了一些建議

1. 使用持續集成,在測試過程中發現運行時錯誤

2. Address Sanitizer和Thread Sanitizer不可兼容,所以不能同時使用

Runtime工具有一定的Overhead,具體如下:

技術分享

參考資料:

Finding Bugs Using Xcode Runtime Tools

Clang Documentation for Address Sanitizer

Clang Documentation for Thread Sanitizer

Clang Documentation for Undefined Behavior Sanitizer

Code Diagnostics

Undefined Behavior Sanitizer

Debugging with Xcode 9

支持了無線Debug,可以不用再需要連接數據線進行真機開發工作

技術分享

增強的斷點:支持條件斷點,並且可以在斷點的時候執行額外語句

技術分享

ViewController Debugging: 可以在查看View Hierarchy時候可以查看到ViewController的信息

技術分享

參考資料:

Debugging with Xcode 9

Localizing with Xcode 9

String Management

使用NSLocalizedString加載多語言,使用localizedStringWithFormat加載格式化的多語言。

// Set a label‘s text
label.text = "Population"
// Set a label‘s text to a localized string
label.text = NSLocalizedString("Population", comment: "Label preceding the population value")
// Load localized string from a specific table
 
label.text = NSLocalizedString("Population", tableName:
  nil, comment:
"Label preceding the population value"
// Create a formatted string
    "Localizable", bundle: .main, value:
)
let
format = NSLocalizedString("%d popular languages", comment:
"Number of popular languages")
label.text = String.localizedStringWithFormat(format, popularLanguages.count)

使用靜態分析可以幫助找到沒有Localized的文本,在Build Setting中勾選上Missing Localizability和Missing Localization Context Comment。

技術分享

靜態資源在項目中的組織如下:

Base.lproj: 基礎資源包

en.lproj: 英文的文本資源

技術分享

Stringsdict

可以根據不同場景使用不同的Localized String,例如單復數的情況:

技術分享

Adaptive Strings

可以根據特定條件,顯示不同的Localized String,例如在不同屏幕尺寸下面顯示不同的文本

技術分享

String資源可以支持XLIFF格式的導入導出

參考資料:

Localizing with Xcode 9

What’s New in Testing

Async Testing

可以用於異步的行為測試,通過設置期望條件,然後等待驗證。引入XCTWaiter,通過顯示的方式來指定異步行為的期望。

// Test case waits implicitly
waitForExpectations(timeout: 10) // Test case waits explicitly wait(for: [documentExpectation], timeout: 10) // Waiter instance delegates to test XCTWaiter(delegate: self).wait(for: [documentExpectation], timeout: 10) // Waiter class returns result let result = XCTWaiter.wait(for: [documentExpectation], timeout: 10) if result == .timedOut { // handling the timeout... }

Multi-app

支持多個APP的同時自動測試,通常可以用於:App Groups,Extensions

技術分享

UI Testing Performance

Xcode 9對於UI Testing進行了大量的優化,提升了性能

技術分享

參考資料:

What‘s New in Testing

iOS 11系列 - Xcode 9新特性