Xcode 9最近剛剛發(fā)布,帶來了一系列不錯的新特性,可以更好的幫助到開發(fā)者完成開發(fā)工作。
Xcode Runtime Tool
Xcode 9中有許多Runtime Tool可以幫助開發(fā)者找到代碼錯誤,包括如下:
Main Thread Checker - Xcode 9新引入
Address Sanitizer
Thread Sanitizer
Undefined Behavior Sanitizer
Using Runtime Tools Effectively
Main Thread Checker
Main Thread Checker可以幫助開發(fā)者找到不在主線程中執(zhí)行的UI操作。

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

在運行時刻,如果發(fā)現(xiàn)有任何的Main Thread問題,則會提示如下:

Address Sanitizer
Address Sanitizer可以用于檢測內(nèi)存問題,Address Sanitizer開啟會帶來比較大的Overhead,所以需要開發(fā)者手動設(shè)置。

開啟之后一旦發(fā)現(xiàn)有任何內(nèi)存問題,就會自動檢測并且提示,如下圖所示:
1. 會有提示告知哪行代碼有使用已經(jīng)釋放的內(nèi)存對象
2. 左邊面板會告知該對象具體的創(chuàng)建、使用、釋放的情況,非常方便debug

Thread Sanitizer
用于發(fā)現(xiàn)多線程問題,在Xcode 9中的Thread Sanitizer可以幫助:
1. 發(fā)現(xiàn)多線程中的數(shù)據(jù)競賽問題
2. 在集合中的數(shù)據(jù)競賽
3. Swift access races
如下圖所示就是Swift關(guān)于Array的數(shù)據(jù)多線程競賽問題:

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

通常解決多線程數(shù)據(jù)競賽問題的方法:
1. 使用GCD同步數(shù)據(jù)操作
2. 使用Serial Queue將共享數(shù)據(jù)的操作串型化
3. Thred Sanitizer是發(fā)現(xiàn)數(shù)據(jù)競賽的很好的工具
Undefined Behaviour Sanitizer
顧名思義,Undefined Behaviour Sanitizer可以幫助開發(fā)者在運行時找到一些異常情況,包括如下情況:
1. 運行時的Bug查找:整型溢出,
2. 檢查C中的不安全的Constructs
3. 和其他運行時的工具可以兼容
Using Runtime Tools Effectively
Apple對于Xcode 9中的Runtime工具提供了一些建議
1. 使用持續(xù)集成,在測試過程中發(fā)現(xiàn)運行時錯誤
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
Debugging with Xcode 9
支持了無線Debug,可以不用再需要連接數(shù)據(jù)線進行真機開發(fā)工作

增強的斷點:支持條件斷點,并且可以在斷點的時候執(zhí)行額外語句

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

參考資料:
Localizing with Xcode 9
String Management
使用NSLocalizedString加載多語言,使用localizedStringWithFormat加載格式化的多語言。
[
](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">
//
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)</pre>
[
](javascript:void(0); "復(fù)制代碼")
使用靜態(tài)分析可以幫助找到?jīng)]有Localized的文本,在Build Setting中勾選上Missing Localizability和Missing Localization Context Comment。

靜態(tài)資源在項目中的組織如下:
Base.lproj: 基礎(chǔ)資源包
en.lproj: 英文的文本資源

Stringsdict
可以根據(jù)不同場景使用不同的Localized String,例如單復(fù)數(shù)的情況:

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

String資源可以支持XLIFF格式的導(dǎo)入導(dǎo)出
參考資料:
What’s New in Testing
Async Testing
可以用于異步的行為測試,通過設(shè)置期望條件,然后等待驗證。引入XCTWaiter,通過顯示的方式來指定異步行為的期望。
[
](javascript:void(0); "復(fù)制代碼")
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">
//
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...
}</pre>
[
](javascript:void(0); "復(fù)制代碼")
Multi-app
支持多個APP的同時自動測試,通??梢杂糜冢篈pp Groups,Extensions

UI Testing Performance
Xcode 9對于UI Testing進行了大量的優(yōu)化,提升了性能

轉(zhuǎn)載自----》
https://www.cnblogs.com/wdsunny/p/7583260.html