xcode9 新增功能

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操作。

image

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

image

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

image

Address Sanitizer

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

image

開啟之后一旦發(fā)現(xiàn)有任何內(nèi)存問題,就會自動檢測并且提示,如下圖所示:

1. 會有提示告知哪行代碼有使用已經(jīng)釋放的內(nèi)存對象

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

image

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ù)多線程競賽問題:

image

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

image

通常解決多線程數(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,具體如下:

image

參考資料:

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,可以不用再需要連接數(shù)據(jù)線進行真機開發(fā)工作

image

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

image

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

image

參考資料:

Debugging with Xcode 9

Localizing with Xcode 9

String Management

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

[
復(fù)制代碼

](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>

[
復(fù)制代碼

](javascript:void(0); "復(fù)制代碼")

使用靜態(tài)分析可以幫助找到?jīng)]有Localized的文本,在Build Setting中勾選上Missing Localizability和Missing Localization Context Comment。

image

靜態(tài)資源在項目中的組織如下:

Base.lproj: 基礎(chǔ)資源包

en.lproj: 英文的文本資源

image

Stringsdict

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

image

Adaptive Strings

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

image

String資源可以支持XLIFF格式的導(dǎo)入導(dǎo)出

參考資料:

Localizing with Xcode 9

What’s New in Testing

Async Testing

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

[
復(fù)制代碼

](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>

[
復(fù)制代碼

](javascript:void(0); "復(fù)制代碼")

Multi-app

支持多個APP的同時自動測試,通??梢杂糜冢篈pp Groups,Extensions

image

UI Testing Performance

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

image

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容