2016-08-03 3 views
-1

XCodeオーガナイザにクラッシュレポートがたくさんあります。 はここで、ここでのDataManagerコード(ライン106)私はクラッシュの原因が何であるかを理解していないIntcをインクリメントしているときにEXC_BREAKPOINTがクラッシュする

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

されるトレース

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) 

されて、ライン106は、とだけ追加ですモジュロ。 KeywordIndexとkeywords.countは両方ともIntです。 私のiPad mini 2やシミュレータでクラッシュを再現することはできません。 keywords.countがゼロのとき

おかげ

+5

'keywords'が空のときは0で割っています – dan

+0

このメソッドを呼び出すと、キーワードは空ではありません – guillaume

+2

「このメソッドは、キーワードが空ではない」と仮定します。しかし、私たちのプログラマーにはこう言われています。あなたのコードはこの仮定をチェックしません。 'keywords.count'がゼロである可能性を擁護するものではありません。したがって、マーフィーの法則によって、それは何とかゼロになるでしょう。 – matt

答えて

1

は、あなたは、例外が発生する効果で、ゼロによる除算を実行しています。

keywordsの配列に少なくとも1つのメンバーがあることを確認するか、countがゼロの場合は1を法とする必要があります。

+0

通常、このコードに達すると、キーワードは空になることはできません...私はコード内で何かが起きたかどうかを確認しようとしています。明確なキーワード – guillaume

+0

このようなチェックを実行したり、例外/エラー。また、いくつかのテストのための素晴らしい候補者です。 – ff10

関連する問題