递增Int时EXC_BREAKPOINT崩溃

时间:2016-08-03 16:56:48

标签: ios swift

我的XCode Organizer中有很多崩溃报告。 这是跟踪

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或模拟器中重现崩溃。

由于

1 个答案:

答案 0 :(得分:1)

keywords.count为零时,您正在执行除零,这会导致异常。

如果keywords为零,您需要检查count数组是否至少有一个成员,还是执行模数1。