EXC_BAD_ACCESS(代码=1,地址=0x0)

EXC_BAD_ACCESS (code=1, address=0x0)

我有一段代码可以在真实设备中正确运行,但在模拟器中出现 EXC_BAD_ACCESS (code=1, address=0x0) 错误 (iphone 6):

bt 输出为:

(lldb) bt
* thread #1: tid = 0xe40f6, 0x00000001052ab5e9 CoreFoundation`CFArrayGetCount + 25, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001052ab5e9 CoreFoundation`CFArrayGetCount + 25
  * frame #1: 0x0000000104c6a5af MyApp`static MyApp.SSID.fetchSSIDInfo (self=MyApp.SSID)() -> Swift.String + 95 at WifiGetter.swift:17
    frame #2: 0x0000000104c4994a MyApp`MyApp.TableViewController.sendWifi (self=0x00007ff05a64da80)() -> () + 26 at TableViewController.swift:355
    frame #3: 0x0000000104c49316 MyApp`MyApp.TableViewController.fetchData (self=0x00007ff05a64da80)() -> () + 38 at TableViewController.swift:308
    frame #4: 0x0000000104c4567f MyApp`MyApp.TableViewController.viewDidLoad (self=0x00007ff05a64da80)() -> () + 3215 at TableViewController.swift:66
    frame #5: 0x0000000104c45ea2 MyApp`@objc MyApp.TableViewController.viewDidLoad (MyApp.TableViewController)() -> () + 34 at TableViewController.swift:0
    frame #6: 0x00000001063c9931 UIKit`-[UIViewController loadViewIfRequired] + 1344
    frame #7: 0x000000010640cc26 UIKit`-[UINavigationController _layoutViewController:] + 54
    frame #8: 0x000000010640d4dd UIKit`-[UINavigationController _updateScrollViewFromViewController:toViewController:] + 433
    frame #9: 0x000000010640d633 UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 116
    frame #10: 0x000000010640e879 UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 890
    frame #11: 0x000000010640f67d UIKit`-[UINavigationController __viewWillLayoutSubviews] + 57
    frame #12: 0x00000001065a763d UIKit`-[UILayoutContainerView layoutSubviews] + 248
    frame #13: 0x00000001062ef11c UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
    frame #14: 0x000000010c35836a QuartzCore`-[CALayer layoutSublayers] + 146
    frame #15: 0x000000010c34cbd0 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 366
    frame #16: 0x000000010c34ca4e QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
    frame #17: 0x000000010c3411d5 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 277
    frame #18: 0x000000010c36e9f0 QuartzCore`CA::Transaction::commit() + 508
    frame #19: 0x000000010c36f154 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
    frame #20: 0x00000001053079d7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #21: 0x0000000105307947 CoreFoundation`__CFRunLoopDoObservers + 391
    frame #22: 0x00000001052fcebc CoreFoundation`CFRunLoopRunSpecific + 524
    frame #23: 0x000000010623998d UIKit`-[UIApplication _run] + 402
    frame #24: 0x000000010623e676 UIKit`UIApplicationMain + 171
    frame #25: 0x0000000104c20efd MyApp`main + 109 at AppDelegate.swift:20
    frame #26: 0x0000000108ea592d libdyld.dylib`start + 1

interfaces 为零。你必须处理这个案子。我建议避免 !。在此处设置类型 non-optional 将允许 if let 绑定正常工作。

您应该将 if let 子句更改为 if let interfaces = CNCopySupportedInterfaces() as? CFArray

因为您使用 if let interfaces: CFArray!! 意味着强制展开,因此,当 CNCopySupportedInterfaces() return nil 或无法转换为 [= 的值时16=], 强制展开会导致崩溃