这种奇怪的崩溃只发生在设备上,而在模拟器中正常工作。 逐行调试,我注意到,当我将一个字符串值附加到双打数组时会发生这种情况。要做到这一点,我使用扩展转换双:
extension String {
func toDouble() -> Double? {
return NSNumberFormatter().numberFromString(self)?.doubleValue
}
}
崩溃的行是:
litros.append(temp_litros.toDouble()!) //litros is my array and temp_litros my string
错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
我真的不明白为什么它在模拟器中有效,而且它不在设备上。
任何人都有想法可能会发生什么?提前谢谢!
崩溃报告:
Incident Identifier: 49F378A2-E7D9-4985-B208-99F529AE8813
CrashReporter Key: fe58b29412047d4d8980b9cb66ae017f1c2db7d5
Hardware Model: iPhone7,2
Process: Nuvem [9479]
Path: /private/var/mobile/Containers/Bundle/Application/8989627A-4064-428C-888B-C09F00A3EA0B/Nuvem.app/Nuvem
Identifier: br.com.gimenez.Nuvem
Version: 1 (1.0)
Code Type: ARM-64 (Native)
Parent Process: launchd [1]
Date/Time: 2015-07-23 18:07:12.529 -0300
Launch Time: 2015-07-23 18:07:05.037 -0300
OS Version: iOS 8.4 (12H143)
Report Version: 105
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000100659474
Triggered by Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libswiftCore.dylib 0x0000000100659474 0x100598000 + 791668
1 Nuvem 0x00000001000b5110 Nuvem.GraficoViewController.viewDidLoad (Nuvem.GraficoViewController)() -> () (GraficoViewController.swift:69)
2 Nuvem 0x00000001000b5410 @objc Nuvem.GraficoViewController.viewDidLoad (Nuvem.GraficoViewController)() -> () (GraficoViewController.swift:0)
3 UIKit 0x0000000188d14c14 -[UIViewController loadViewIfRequired] + 688
4 UIKit 0x0000000188d14924 -[UIViewController view] + 28
5 UIKit 0x0000000188eb71ec -[UINavigationController _startCustomTransition:] + 708
6 UIKit 0x0000000188dc9e80 -[UINavigationController _startDeferredTransitionIfNeeded:] + 464
7 UIKit 0x0000000188dc9c4c -[UINavigationController __viewWillLayoutSubviews] + 52
8 UIKit 0x0000000188dc9bcc -[UILayoutContainerView layoutSubviews] + 196
9 UIKit 0x0000000188d116f0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 576
10 QuartzCore 0x000000018864ddb4 -[CALayer layoutSublayers] + 148
11 QuartzCore 0x000000018864881c CA::Layer::layout_if_needed(CA::Transaction*) + 316
12 QuartzCore 0x00000001886486c0 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 28
13 QuartzCore 0x0000000188647e54 CA::Context::commit_transaction(CA::Transaction*) + 272
14 QuartzCore 0x0000000188647bd4 CA::Transaction::commit() + 524
15 QuartzCore 0x00000001886412fc CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 76
16 CoreFoundation 0x00000001842542a0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 28
17 CoreFoundation 0x000000018425122c __CFRunLoopDoObservers + 356
18 CoreFoundation 0x000000018425160c __CFRunLoopRun + 832
19 CoreFoundation 0x000000018417d2d0 CFRunLoopRunSpecific + 392
20 GraphicsServices 0x000000018dbd36f8 GSEventRunModal + 164
21 UIKit 0x0000000188d7af3c UIApplicationMain + 1484
22 Nuvem 0x00000001000ad9b8 main (AppDelegate.swift:14)
23 libdyld.dylib 0x0000000196612a04 start + 0
Thread 1 name: Dispatch queue: com.apple.libdispatch-manager
Thread 1:
0 libsystem_kernel.dylib 0x0000000196710c24 kevent64 + 8
1 libdispatch.dylib 0x00000001965f5e6c _dispatch_mgr_invoke + 272
2 libdispatch.dylib 0x00000001965e7998 _dispatch_mgr_thread + 48
Thread 2:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 3:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 4:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 5:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 6 name: com.apple.NSURLConnectionLoader
Thread 6:
0 libsystem_kernel.dylib 0x0000000196710e0c mach_msg_trap + 8
1 libsystem_kernel.dylib 0x0000000196710c84 mach_msg + 68
2 CoreFoundation 0x0000000184253720 __CFRunLoopServiceMachPort + 196
3 CoreFoundation 0x0000000184251674 __CFRunLoopRun + 936
4 CoreFoundation 0x000000018417d2d0 CFRunLoopRunSpecific + 392
5 CFNetwork 0x0000000183c5a590 +[NSURLConnection(Loader) _resourceLoadLoop:] + 436
6 Foundation 0x000000018519ddb4 __NSThread__main__ + 1068
7 libsystem_pthread.dylib 0x00000001967c7dc4 _pthread_body + 160
8 libsystem_pthread.dylib 0x00000001967c7d20 _pthread_start + 156
9 libsystem_pthread.dylib 0x00000001967c4ef4 thread_start + 0
Thread 7 name: com.apple.CFSocket.private
Thread 7:
0 libsystem_kernel.dylib 0x000000019672b498 __select + 8
1 CoreFoundation 0x0000000184259124 __CFSocketManager + 668
2 libsystem_pthread.dylib 0x00000001967c7dc4 _pthread_body + 160
3 libsystem_pthread.dylib 0x00000001967c7d20 _pthread_start + 156
4 libsystem_pthread.dylib 0x00000001967c4ef4 thread_start + 0
Thread 8:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 9:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 10:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 11:
0 libsystem_kernel.dylib 0x000000019672bc78 __workq_kernreturn + 8
1 libsystem_pthread.dylib 0x00000001967c52d8 _pthread_wqthread + 988
2 libsystem_pthread.dylib 0x00000001967c4eec start_wqthread + 0
Thread 12:
0 libsystem_kernel.dylib 0x0000000196710e0c mach_msg_trap + 8
1 libsystem_kernel.dylib 0x0000000196710c84 mach_msg + 68
2 CoreFoundation 0x0000000184253720 __CFRunLoopServiceMachPort + 196
3 CoreFoundation 0x0000000184251674 __CFRunLoopRun + 936
4 CoreFoundation 0x000000018417d2d0 CFRunLoopRunSpecific + 392
5 Foundation 0x00000001850b54c4 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 312
6 Foundation 0x000000018510f80c -[NSRunLoop(NSRunLoop) run] + 92
7 Nuvem 0x00000001000e64f8 +[PBAFURLConnectionOperation networkRequestThreadEntryPoint:] (PBAFURLConnectionOperation.m:151)
8 Foundation 0x000000018519ddb4 __NSThread__main__ + 1068
9 libsystem_pthread.dylib 0x00000001967c7dc4 _pthread_body + 160
10 libsystem_pthread.dylib 0x00000001967c7d20 _pthread_start + 156
11 libsystem_pthread.dylib 0x00000001967c4ef4 thread_start + 0
Thread 0 crashed with ARM Thread State (64-bit):
x0: 0x0000000100588b80 x1: 0x00000001742f6a00 x2: 0x0000000000000008 x3: 0x0000000196765a74
x4: 0x000000016fd80e38 x5: 0x0000000000000020 x6: 0x0000000000000000 x7: 0x0000000000000fa0
x8: 0x00000001742f6b80 x9: 0x0000000000000000 x10: 0x0000000100584800 x11: 0x00000000005d0bda
x12: 0x00000000fffffff2 x13: 0x00000001887c5480 x14: 0x0000000000000053 x15: 0x000000000000004d
x16: 0x00000001967bda50 x17: 0x0000000000000000 x18: 0x0000000000000000 x19: 0x0000000144d85300
x20: 0x00000001978b7720 x21: 0x0000000000000008 x22: 0x0000000000000008 x23: 0x0000000000000000
x24: 0x0000000000000006 x25: 0x000000000004ffff x26: 0x0000000144e161d0 x27: 0x000000016fd810e9
x28: 0x0000000144d85300 fp: 0x000000016fd80ed0 lr: 0x0000000100659474
sp: 0x000000016fd80ec0 pc: 0x0000000100659474 cpsr: 0x60000000
答案 0 :(得分:1)
默认情况下,NSNumberFormatter
会根据当前的区域设置转换数据。在模拟器的情况下,它使用美国语言环境和"。"作为小数分隔符。如果您的设备使用","作为分隔符,因此它无法转换您的数据,numberFromString
将返回nil。
试试这个:
extension String {
func toDouble() -> Double? {
let formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US")
return formatter.numberFromString(self)?.doubleValue
}
}