2016-08-03 71 views
-1

我的XCode Organizer中有很多崩潰報告。 這裏是跟蹤EXC_BREAKPOINT在增加Int時崩潰

Exception Type: EXC_BREAKPOINT (SIGTRAP) 
Exception Codes: 0x0000000000000001, 0x00000001000746a8 
Triggered by Thread: 0 

Breadcrumb Trail: (reverse chronological seconds) 
9  GC Framework: startAuthenticationForExistingPrimaryPlayer 


Thread 0 name: 
Thread 0 Crashed: 
0 MyApp      0x00000001000746a8 DataManager.incrementKeywordIndexByTwo() ->() + 284 (DataManager.swift:106) 
1 MyApp      0x000000010007995c @objc ViewController.unwindToMainMenu(UIStoryboardSegue) ->() + 68 (ViewController.swift:50) 
2 UIKit       0x00000001874ac16c -[UIStoryboardUnwindSegueTemplate _performWithDestinationViewController:sender:] + 216 (UIStoryboardUnwindSegueTemplate.m:471) 
3 UIKit       0x00000001874ac05c -[UIStoryboardUnwindSegueTemplate _perform:] + 92 (UIStoryboardUnwindSegueTemplate.m:458) 
4 UIKit       0x00000001872fe184 -[UIStoryboardSegueTemplate perform:] + 160 (UIStoryboardSegueTemplate.m:123) 
5 UIKit       0x0000000186becbe8 -[UIApplication sendAction:to:from:forEvent:] + 100 (UIApplication.m:4265) 
6 UIKit       0x0000000186becb64 -[UIControl sendAction:to:forEvent:] + 80 (UIControl.m:612) 
7 UIKit       0x0000000186bd4870 -[UIControl _sendActionsForEvents:withEvent:] + 436 (UIControl.m:694) 
8 UIKit       0x0000000186bec454 -[UIControl touchesEnded:withEvent:] + 572 (UIControl.m:446) 
9 MyApp      0x0000000100081fe0 specialized GameButton.touchesEnded(Set<UITouch>, withEvent : UIEvent?) ->() + 188 (GameButton.swift:85) 
10 MyApp      0x000000010008183c @objc GameButton.touchesEnded(Set<UITouch>, withEvent : UIEvent?) ->() + 128 (GameButton.swift:0) 
11 UIKit       0x0000000186bec084 -[UIWindow _sendTouchesForEvent:] + 804 (UIWindow.m:2135) 
12 UIKit       0x0000000186be4c20 -[UIWindow sendEvent:] + 784 (UIWindow.m:2257) 
13 UIKit       0x0000000186bb504c -[UIApplication sendEvent:] + 248 (UIApplication.m:12647) 
14 UIKit       0x0000000186bb3628 _UIApplicationHandleEventQueue + 6568 (UIApplication.m:10454) 
15 CoreFoundation     0x0000000181a0d09c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1761) 
16 CoreFoundation     0x0000000181a0cb30 __CFRunLoopDoSources0 + 540 (CFRunLoop.c:1807) 
17 CoreFoundation     0x0000000181a0a830 __CFRunLoopRun + 724 (CFRunLoop.c:2536) 
18 CoreFoundation     0x0000000181934c50 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814) 
19 GraphicsServices    0x000000018321c088 GSEventRunModal + 180 (GSEvent.c:2245) 
20 UIKit       0x0000000186c1e088 UIApplicationMain + 204 (UIApplication.m:3772) 
21 MyApp      0x0000000100083920 main + 152 (AppDelegate.swift:14) 
22 libdyld.dylib     0x00000001814d28b8 start + 4 (start_glue.s:78) 

這裏是DataManager的代碼(線106)

var keywords = [Keyword]() 
var keywordIndex = 0 
func incrementKeywordIndexByTwo() { 
    keywordIndex = (keywordIndex + 2) % keywords.count 
} 

我不明白什麼是崩潰的原因,線路106僅僅是一個除了用模。 KeywordIndex和keywords.count都是Int。 我無法在iPad mini 2或模擬器中重現崩潰。

由於

+5

當'keywords'爲空時,您被零除 – dan

+0

當這種方法被稱爲關鍵字不爲空 – guillaume

+2

「當這種方法被稱爲關鍵字不是空的」所以你假設。但是我們的程序員有一種說法:不要假設。你的代碼不檢查這個假設;它無法抵禦'keywords.count'爲零的可能性。因此,墨菲定律將以某種方式爲零。 – matt

回答

1

keywords.count爲零,則通過在零效應,這導致異常執行除法。

如果count爲零,則需要檢查keywords陣列是否至少有一個成員或執行模1。

+0

正常情況下,當這段代碼到達時,關鍵字不能爲空......我正在查看是否在我的代碼中發生了什麼情況en清除關鍵字 – guillaume

+0

這是一種很好的做法,可以執行這樣的檢查,並/或引發異常/錯誤。也是一些測試的好選擇。 – ff10