NSError 为 nil 但进入非 nil 状态

Posted

技术标签:

【中文标题】NSError 为 nil 但进入非 nil 状态【英文标题】:NSError is nil but goes into not nil condition 【发布时间】:2015-09-23 18:34:14 【问题描述】:

我的应用程序中有这样的代码,它适用于开发配置文件。但是使用 AdHoc 配置文件,当我尝试打印错误时,我的应用程序崩溃了。根据日志,不应该有任何错误,应该为零。但我不知道为什么它满足 if 条件,如果它满足条件,那么如果我尝试打印错误,为什么它会崩溃。另外,此代码在开发配置文件中运行良好。此问题仅与 AdHoc 配置文件有关。 -

func initializedWithCredentials( credentials: GTICredentials!, error: NSError ) 
 // Check to see if initialization was successful. With log i can see that initialization was successful only so there should not be any error
if let error = error as NSError? 
      // Handle error
      NSLog("Error : \(error)")     // App Crashes 
else
      // Success so do something     


下面是设备日志——

Incident Identifier: 17FD2BD0-4FA5-492B-891C-2EC066EAFC41
CrashReporter Key:   0fd3245f5726c1a1a90046a6629d5ccc9ebf39dd
Hardware Model:      iPhone4,1
Process:             VidurOnMobile [2160]
Path:                /private/var/mobile/Containers/Bundle/Application/A945905D-C112-4E96-9124-775F4590E5FB/VidurOnMobile.app/VidurOnMobile
Identifier:          com.ibm.cio.be.iphone.VidurOnMobile
Version:             1 (1.0)
Code Type:           ARM (Native)
Parent Process:      launchd [1]

Date/Time:           2015-09-22 22:25:45.149 +0530
Launch Time:         2015-09-22 22:25:01.041 +0530
OS Version:          ios 8.4.1 (12H321)
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000000
Triggered by Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   CoreFoundation                  0x2a8bf0d6 0x2a89c000 + 143574
1   libswiftCore.dylib              0x00962960 0x886000 + 903520
2   libswiftCore.dylib              0x00943160 0x886000 + 774496
3   VidurOnMobile                   0x00107728 0xea000 + 120616
4   VidurOnMobile                   0x00109df8 0xea000 + 130552
5   libdispatch.dylib               0x393c9c80 0x393c8000 + 7296
6   libdispatch.dylib               0x393c9c6c 0x393c8000 + 7276
7   libdispatch.dylib               0x393d554e 0x393c8000 + 54606
8   CoreFoundation                  0x2a96c664 0x2a89c000 + 853604
9   CoreFoundation                  0x2a96ad84 0x2a89c000 + 847236
10  CoreFoundation                  0x2a8b66cc 0x2a89c000 + 108236
11  CoreFoundation                  0x2a8b64de 0x2a89c000 + 107742
12  GraphicsServices                0x3227b1a4 0x32272000 + 37284
13  UIKit                           0x2e068440 0x2dff9000 + 455744
14  VidurOnMobile                   0x000fa62e 0xea000 + 67118
15  libdyld.dylib                   0x39407aac 0x39406000 + 6828

Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 1:
0   libsystem_kernel.dylib          0x394ba24c 0x394b9000 + 4684
1   libdispatch.dylib               0x393da4ec 0x393c8000 + 74988
2   libdispatch.dylib               0x393da21e 0x393c8000 + 74270

Thread 2:
0   libsystem_kernel.dylib          0x394ce9c0 0x394b9000 + 88512
1   libsystem_pthread.dylib         0x3954be14 0x3954b000 + 3604
2   libsystem_pthread.dylib         0x3954baec 0x3954b000 + 2796

Thread 3 name:  Dispatch queue: com.apple.root.default-qos.overcommit
Thread 3:
0   CoreFoundation                  0x2a8aa348 0x2a89c000 + 58184
1   CoreFoundation                  0x2a9dbc2c 0x2a89c000 + 1309740
2   CoreFoundation                  0x2a8aa344 0x2a89c000 + 58180
3   libobjc.A.dylib                 0x38e541a4 0x38e33000 + 135588
4   libdispatch.dylib               0x393d69b4 0x393c8000 + 59828
5   libdispatch.dylib               0x393d789e 0x393c8000 + 63646
6   libsystem_pthread.dylib         0x3954bd9a 0x3954b000 + 3482
7   libsystem_pthread.dylib         0x3954baec 0x3954b000 + 2796

Thread 4:
0   libsystem_kernel.dylib          0x394ce9c0 0x394b9000 + 88512
1   libsystem_pthread.dylib         0x3954be14 0x3954b000 + 3604
2   libsystem_pthread.dylib         0x3954baec 0x3954b000 + 2796

Thread 5 name:  Dispatch queue: com.apple.CFNetwork.addPersistCacheToStorageDaemon
Thread 5:
0   libsystem_kernel.dylib          0x394ba49c 0x394b9000 + 5276
1   libsystem_kernel.dylib          0x394ba290 0x394b9000 + 4752
2   libdispatch.dylib               0x393dae1a 0x393c8000 + 77338
3   libdispatch.dylib               0x393dab46 0x393c8000 + 76614
4   libxpc.dylib                    0x39560ea6 0x39560000 + 3750
5   Foundation                      0x2b7a98fa 0x2b617000 + 1648890
6   Foundation                      0x2b7a8f6e 0x2b617000 + 1646446
7   Foundation                      0x2b62abea 0x2b617000 + 80874
8   CoreFoundation                  0x2a9aa30c 0x2a89c000 + 1106700
9   CoreFoundation                  0x2a8d9704 0x2a89c000 + 251652
10  CFNetwork                       0x2a462e98 0x2a3d4000 + 585368
11  CFNetwork                       0x2a43179c 0x2a3d4000 + 382876
12  libdispatch.dylib               0x393c9c80 0x393c8000 + 7296
13  libdispatch.dylib               0x393d44ce 0x393c8000 + 50382
14  libdispatch.dylib               0x393d3d9c 0x393c8000 + 48540
15  libdispatch.dylib               0x393d648e 0x393c8000 + 58510
16  libdispatch.dylib               0x393d789e 0x393c8000 + 63646
17  libsystem_pthread.dylib         0x3954bd9a 0x3954b000 + 3482
18  libsystem_pthread.dylib         0x3954baec 0x3954b000 + 2796

Thread 6 name:  WebThread
Thread 6:
0   libsystem_kernel.dylib          0x394ba49c 0x394b9000 + 5276
1   libsystem_kernel.dylib          0x394ba290 0x394b9000 + 4752
2   CoreFoundation                  0x2a96c5ce 0x2a89c000 + 853454
3   CoreFoundation                  0x2a96ab94 0x2a89c000 + 846740
4   CoreFoundation                  0x2a8b66cc 0x2a89c000 + 108236
5   CoreFoundation                  0x2a8b64de 0x2a89c000 + 107742
6   WebCore                         0x36723d2c 0x366a2000 + 531756
7   libsystem_pthread.dylib         0x3954dddc 0x3954b000 + 11740
8   libsystem_pthread.dylib         0x3954dd4e 0x3954b000 + 11598
9   libsystem_pthread.dylib         0x3954baf8 0x3954b000 + 2808

Thread 7 name:  javascriptCore::BlockFree
Thread 7:
0   libsystem_kernel.dylib          0x394cdb2c 0x394b9000 + 84780
1   libsystem_pthread.dylib         0x3954d354 0x3954b000 + 9044
2   libsystem_pthread.dylib         0x3954e224 0x3954b000 + 12836
3   libc++.1.dylib                  0x38600fa4 0x385fb000 + 24484
4   JavaScriptCore                  0x2bbd3206 0x2bbbe000 + 86534
5   JavaScriptCore                  0x2bbcf6bc 0x2bbbe000 + 71356
6   libsystem_pthread.dylib         0x3954dddc 0x3954b000 + 11740
7   libsystem_pthread.dylib         0x3954dd4e 0x3954b000 + 11598
8   libsystem_pthread.dylib         0x3954baf8 0x3954b000 + 2808

Thread 8 name:  JavaScriptCore::Marking
Thread 8:
0   libsystem_kernel.dylib          0x394cdb2c 0x394b9000 + 84780
1   libsystem_pthread.dylib         0x3954d354 0x3954b000 + 9044
2   libsystem_pthread.dylib         0x3954e224 0x3954b000 + 12836
3   libc++.1.dylib                  0x38600fa4 0x385fb000 + 24484
4   JavaScriptCore                  0x2bd98140 0x2bbbe000 + 1941824
5   JavaScriptCore                  0x2bd981a4 0x2bbbe000 + 1941924
6   JavaScriptCore                  0x2bbcf6bc 0x2bbbe000 + 71356
7   libsystem_pthread.dylib         0x3954dddc 0x3954b000 + 11740
8   libsystem_pthread.dylib         0x3954dd4e 0x3954b000 + 11598
9   libsystem_pthread.dylib         0x3954baf8 0x3954b000 + 2808

Thread 9 name:  com.apple.NSURLConnectionLoader
Thread 9:
0   libsystem_kernel.dylib          0x394ba49c 0x394b9000 + 5276
1   libsystem_kernel.dylib          0x394ba290 0x394b9000 + 4752
2   CoreFoundation                  0x2a96c5ce 0x2a89c000 + 853454
3   CoreFoundation                  0x2a96ab94 0x2a89c000 + 846740
4   CoreFoundation                  0x2a8b66cc 0x2a89c000 + 108236
5   CoreFoundation                  0x2a8b64de 0x2a89c000 + 107742
6   CFNetwork                       0x2a451422 0x2a3d4000 + 513058
7   Foundation                      0x2b6e7186 0x2b617000 + 852358
8   libsystem_pthread.dylib         0x3954dddc 0x3954b000 + 11740
9   libsystem_pthread.dylib         0x3954dd4e 0x3954b000 + 11598
10  libsystem_pthread.dylib         0x3954baf8 0x3954b000 + 2808

Thread 10:
0   libsystem_kernel.dylib          0x394ce9c0 0x394b9000 + 88512
1   libsystem_pthread.dylib         0x3954be14 0x3954b000 + 3604
2   libsystem_pthread.dylib         0x3954baec 0x3954b000 + 2796

Thread 11:
0   libsystem_kernel.dylib          0x394ce9c0 0x394b9000 + 88512
1   libsystem_pthread.dylib         0x3954be14 0x3954b000 + 3604
2   libsystem_pthread.dylib         0x3954baec 0x3954b000 + 2796

Thread 12 name:  com.apple.CFSocket.private
Thread 12:
0   libsystem_kernel.dylib          0x394ce080 0x394b9000 + 86144
1   CoreFoundation                  0x2a970d80 0x2a89c000 + 871808
2   libsystem_pthread.dylib         0x3954dddc 0x3954b000 + 11740
3   libsystem_pthread.dylib         0x3954dd4e 0x3954b000 + 11598
4   libsystem_pthread.dylib         0x3954baf8 0x3954b000 + 2808

Thread 0 crashed with ARM Thread State (32-bit):
    r0: 0x00000000    r1: 0x00000000      r2: 0x00000000      r3: 0x00000004
    r4: 0x00000000    r5: 0x00000000      r6: 0x00d416c0      r7: 0x00884c44
    r8: 0x1768cf00    r9: 0x175b8774     r10: 0x00884cf0     r11: 0x3bded0e0
    ip: 0x0068c2e0    sp: 0x00884c18      lr: 0x00962964      pc: 0x2a8bf0d6
  cpsr: 0x60000030

【问题讨论】:

从函数的定义来看,error不能为nil。错误:NSError error 是可选的,因此它可以是 nil 或一些错误。在这种情况下,不应该有任何错误,应该进入 else 条件,但它会进入 if 条件,如果尝试打印错误也会崩溃。此外,此问题仅与 AdHoc 配置文件有关。 【参考方案1】:

如果函数的定义是错误的,则错误永远不能为零:NSError。如果错误可能为零,则应将其定义为错误:NSError?。否则,尝试解开非可选值是没有意义的。它应该是这样的:

func initializedWithCredentials(credentials: GTICredentials!, error: NSError? ) 

    if let error = error 
      // Handle error
       NSLog("Error : \(error)")
     else 
      // Success so do something     
    

更好的是,你应该避免嵌套 if 条件

func initializedWithCredentials(credentials: GTICredentials!, error: NSError? ) 

    if let error = error 
       // Handle error
       NSLog("Error : \(error)")
       return
     

    // Success so do something     

【讨论】:

以上是关于NSError 为 nil 但进入非 nil 状态的主要内容,如果未能解决你的问题,请参考以下文章

自定义 NSError 奇怪的行为 Swift

当应用程序进入后台时,URLSessions 给 URLResponse nil

从 MPMediaItems 获取 iCloud 状态信息

iOS 9:SecItemCopyMatching 返回成功状态码,但键为 nil

解决 pathForResource 返回 nil的问题

OC中的指针