问题
I'm struggling to understand how a program can crash on a line with just a bracket. My open-source app, Steppy 2, is having this problem. I have been unable to replicate this myself, but multiple users have reported this problem and have sent crash reports. The crash occurs on the last bracket:
func loadBars() {
let graphView = UIView(frame: CGRectZero)
graphView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addSubview(graphView)
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: nil, metrics: nil, views: ["view":graphView]))
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]-(30)-|", options: nil, metrics: nil, views: ["view":graphView]))
let sortedDayKeys = Array(weekData.keys).sorted(<)
for var day = 0; day < 7; day++ {
let key = sortedDayKeys[day] as String
let bar = STPYGraphBar(frame: CGRectZero)
bar.dateKey = key
bar.steps = weekData[key]
bar.divider = divider
bar.backgroundColor = UIColor.clearColor()
graphView.addSubview(bar)
graphView.bringSubviewToFront(bar)
bar.createInnerView()
addBarContraints(bar)
graphView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[bar]|", options: nil, metrics: nil, views: ["bar":bar]))
bars.append(bar)
}
graphView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[bar1][bar2(bar1)][bar3(bar1)][bar4(bar1)][bar5(bar1)][bar6(bar1)][bar7(bar1)]|", options: nil, metrics: nil, views: ["bar1":bars[0],"bar2":bars[1],"bar3":bars[2],"bar4":bars[3],"bar5":bars[4],"bar6":bars[5],"bar7":bars[6]]))
} //HERE
The crash information:
Thread : Crashed: com.apple.main-thread
0 SwiftSteppy 0x0000000100094b2c SwiftSteppy.STPYGraphView.loadBars (SwiftSteppy.STPYGraphView)() -> () (STPYGraphView.swift:108)
1 SwiftSteppy 0x0000000100094100 SwiftSteppy.STPYGraphView.loadBars (SwiftSteppy.STPYGraphView)() -> () (STPYGraphView.swift)
2 SwiftSteppy 0x000000010009100c SwiftSteppy.STPYGraphView.loadWithProgress (SwiftSteppy.STPYGraphView)(CoreGraphics.CGFloat) -> () (STPYGraphView.swift:32)
3 SwiftSteppy 0x0000000100069a34 SwiftSteppy.STPYGraphViewController.loadMainScrollViewContent (SwiftSteppy.STPYGraphViewController)() -> () (STPYGraphViewController.swift:201)
4 SwiftSteppy 0x0000000100067abc SwiftSteppy.STPYGraphViewController.loadDataInterface (SwiftSteppy.STPYGraphViewController)() -> () (STPYGraphViewController.swift:139)
5 SwiftSteppy 0x00000001000713e0 SwiftSteppy.STPYGraphViewController.(loadData (SwiftSteppy.STPYGraphViewController) -> () -> ()).(closure #1).(closure #1) (STPYGraphViewController.swift:76)
6 libdispatch.dylib 0x000000019866d3ac _dispatch_call_block_and_release + 24
7 libdispatch.dylib 0x000000019866d36c _dispatch_client_callout + 16
8 libdispatch.dylib 0x0000000198671980 _dispatch_main_queue_callback_4CF + 932
9 CoreFoundation 0x00000001878e1fa4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
10 CoreFoundation 0x00000001878e004c __CFRunLoopRun + 1492
11 CoreFoundation 0x000000018780d0a4 CFRunLoopRunSpecific + 396
12 GraphicsServices 0x00000001909af5a4 GSEventRunModal + 168
13 UIKit 0x000000018c13eaa4 UIApplicationMain + 1488
14 SwiftSteppy 0x00000001000a5da8 main (STPYAppDelegate.swift:13)
15 libdyld.dylib 0x0000000198696a08 start + 4
The full crash information can be found here and the code context here with the line where the crash occurs highlighted.
回答1:
Apparently, swift crash reports don't indicate the exact line the app crashed on, unlike objective-c apps. This means some line in the method triggered the crash, not the final bracket.
来源:https://stackoverflow.com/questions/28778177/swift-crash-on-a-line-that-only-has-a-bracket