Xcode Runtime Tool

作者: 分流替躺欧阳克 | 来源:发表于2018-04-17 10:13 被阅读185次

    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 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:

    )

    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) ifresult == .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新特性

    标签:iossectionadacolortinrsazab方便localize

    原文地址:http://www.cnblogs.com/wdsunny/p/7583260.html

    相关文章

      网友评论

        本文标题:Xcode Runtime Tool

        本文链接:https://www.haomeiwen.com/subject/wrlukftx.html