在Xcode中使用Breakpoint來debug是非常平常的事情,優秀的我們都能從Breakpoint來得知上下文狀態,如各個變數值。有時候崩潰(Crash)來得突然,Xcode沒有提供詳細的Crash訊息,像是我這個例子是:存取Empty Array。
此時例外中斷點 (Exception Breakpoint)就非常好用!
存取Empty Array這個Crash,Xcode只丟出訊息:
2018-04-17 15:33:41.326212+0800 TestCoreML[55815:9713977] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray’
*** First throw call stack:
(0x18288fd38 0x181da4528 0x1827eb1d8 0x102ed4a04 0x102ed1c6c 0x18bcb769c 0x18bd6030c 0x18bcb769c 0x18bd210c8 0x18bd21064 0x18bf0f788 0x18bf02b24 0x18bf1aaa0 0x18283816c 0x182837a3c 0x18283574c 0x1827562d8 0x1845e7f84 0x18bd02880 0x102ee843c 0x18227a56c)
libc++abi.dylib: terminating with uncaught exception of type NSException
知道是存取Empty Array問題,但是我Array像沙子那麼多,哪裡找到有問題的code?
想起超級大絕招例外中斷點 (Exception Breakpoint)!
打開異常中斷點後,會發現上頭多了三個Breakpoint,也就是若有難纏的例外會丟出來讓你知道!
終於知道是哪一行,然後追回去看資料出了什麼問題,原來是讀到非預期的數字,以致於產生空陣列。
其它難找的Crash也可以透過異常中斷點來丟出,試試看就知道多好用!
隨意留個言吧:)~