XCode笔记

  • Xcode自带Open QuicklyCmd+Shift+O
  • 添加Code Snippet:将选中的代码段拖到右边的Code Snippet Library中,将需要被高亮替换的snippet写作<#snippet#>
  • xcode4.x给target添加外部脚本可通过:
    1. Build Phrase: Target->Build Phrases->Add Build Phrase->Add Run Script

    2. Target Dependency: Target->Add Target->External Build System,然后主target的Build Phrases->Target Dependencies中添上它,这个target的Info->External Build Tool Configuration可如下:

       Build Tools: /bin/sh
         Arguments: ./TPupdate.sh
         Directory: ${PROJECT_DIR}/Assets/scripts/
       pass build settins in environment    
  • 给已有项目添加UnitTests,参见Adding Unit Tests to an existing iOS project with Xcode 4。在build target改名后,UnitTests的Bundle Loader项要随之更新。若开启了c++11,确保UnitTests和项目已有target的Build Settings中的C++ Language Dialect和C++ Standard Library设得一样。
  • Method Completion:type - or +, begin typing method name, omit return value
  • XCode6中可用// MARK: 代替#pragma mark,还可用// TODO:// FIXME:

Debug

  • Breakpoint Navigator (cmd+6)的左下方‘+’ -> Add Exception Breakpoint在异常抛出前暂停,当crash在main.m时开启调试

  • Edit Schema -> Diagnostics -> Enabel Zombie Objects,使引用计数为0的对象只标记不释放,当EXC_BAD_ACCESS时开启调试

  • 在代码栏左侧点击添加的breakpoint上右键 -> edit,添加条件后得到conditional breakpoint

  • 可给Breakpoint选择Action:Debugger Command,并勾上Automatically continue after evaluating,使用格式:po object

  • gdb里可用po (print object) 打印对象,po跟NSLog一样调用对象的description方法

    • 给类实现方法- (NSString *)debugDescription;,可在gdb时用po打印或Breakpoint时Log Message输出
    • UIView有方法- (NSString *)recursiveDescription;,可在gdb时输出view的层次结构
  • Xcode5.1+可以给custom object定义- (id)debugQuickLookObject;方法,以在debug quick look时查看

  • 可以在instrument的Timer Profiler里测试background fetch

Simulator

  • Double High Status Bar:测试是否对status bar的高度变化正确响应
  • Color Blended Layers:绿色表示opaque,红色表示含alpha。尽量减少红色以提高性能(尤其是scrolling list中红色)

Launch Arguments & Environment Variables

设置运行参数和环境变量:Edit Scheme -> Run [AppName].app -> Arguments,调试时两者作用相同。设置运行参数,如-NSDoubleLocalizedStrings YES;设置环境变量,则设置Name和Value,如NSZombieEnabled(Name)和YES(Value)

Localization

  • -NSDoubleLocalizedStrings YES:将字符串长度double以测试长标签
  • -NSShowNonLocalizedStrings YES:将无法localized的串以全大写显示,同时有log输出
  • -AppleLanguages (zh):不用再跑设置中更改语言,可同时再设置AppleLocale zh-Hans

Core Data

  • -com.apple.CoreData.SQLDebug 3:输出debug信息,值可设为1到3(1最简略,3最详细)
  • -com.apple.CoreData.SyntaxColoredLogging YES:彩色log
  • -com.apple.CoreData.MigrationDebug:输出migration的debug信息

Tool


Instruments

Time Profiling

  • 选中Record Waiting Threads时,Sample PerspectiveRunning Sample Times以避免显示太多idle时间
  • drag: apply a time range filter, shift+drag: zoom in, ctrl+drag: zoom out

References