iOS 11系列 - Xcode 9新特性

來源:互聯網
上載者:User

標籤:ios   section   ada   color   tin   rsa   zab   方便   localize   

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 9String Management

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

// Set a label‘s textlabel.text = "Population"// Set a label‘s text to a localized stringlabel.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:)letformat = 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新特性

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.