libc++abi.dylib:以 NSException 类型的未捕获异常终止 - 我是否需要修复启动屏幕或 Viewcontroller?

libc++abi.dylib: terminating with uncaught exception of type NSException - Do I need to fix the Launch Screen or Viewcontroller?

我正在设计我的第一个应用程序,对 Swift 还很陌生。

我的应用程序将成功构建,但不会通过启动屏幕并给出错误 "libc++abi.dylib: terminating with uncaught exception of type NSException"。我在此发现的其他类似问题我尝试修复但无法使其工作。

我不确定问题是在我的启动屏幕上还是在视图控制器上。

还有关于"The use of Swift 3 @objc inference in Swift 4 mode is deprecated."

的警告

这是导致错误的原因吗?

提前致谢。

启动屏幕代码(App Delegate):

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

查看控制器代码:

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {

//connection that ties search bar in view to input for viewcontroller

@IBOutlet weak var searchbar: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()
    searchbar.delegate = self
}
//activates keyboard etc when searchbar clicked
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    //(dncode) is string that will equal text as entered into search     bar

    let dncode = String()

    searchBar.text! = dncode

    if let url = URL (string: "https://www.example.com/(dncode)")
    {


        //this section to check and auto open URL in default browser     "Safari"
    if #available(iOS 10.0, *)
    {

        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}
}
}

日志:

Terminating app due to uncaught exception 'NSUnknownKeyException',     reason: '[<DNAppBeta.ViewController 0x7fe1edd095e0>   setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key SearchBar.'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010d5ad1cb __exceptionPreprocess + 171

1   libobjc.A.dylib                     0x0000000109189f41 objc_exception_throw + 48

2   CoreFoundation                      0x000000010d5ad119 -[NSException raise] + 9

3   Foundation                          0x0000000108bac1e3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292

4   UIKit                               0x000000010a17f41f -[UIViewController setValue:forKey:] + 87

5   UIKit                               0x000000010a46e9dd -[UIRuntimeOutletConnection connect] + 109

6   CoreFoundation                      0x000000010d55057d -[NSArray makeObjectsPerformSelector:] + 317

7   UIKit                               0x000000010a46d393 -[UINib instantiateWithOwner:options:] + 1856

8   UIKit                               0x000000010a1864e0 -[UIViewController _loadViewFromNibNamed:bundle:] + 383

9   UIKit                               0x000000010a186e0c -[UIViewController loadView] + 177

10  UIKit                               0x000000010a18713d -[UIViewController loadViewIfRequired] + 195

11  UIKit                               0x000000010a18799a -[UIViewController view] + 27

12  UIKit                               0x000000010a055ae3 -[UIWindow addRootViewControllerViewIfPossible] + 122

13  UIKit                               0x000000010a0561eb -[UIWindow _setHidden:forced:] + 294

14  UIKit                               0x000000010a069098 -[UIWindow makeKeyAndVisible] + 42

15  UIKit                               0x0000000109fdb521 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4711

16  UIKit                               0x0000000109fe0751 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1720

17  UIKit                               0x000000010a3a5e00 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 924

18  UIKit                               0x000000010a778840 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153

19  UIKit                               0x000000010a3a59f9 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249

20  UIKit                               0x000000010a3a6257 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 668

21  UIKit                               0x000000010ad19dd9 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 262

22  UIKit                               0x000000010ad19c92 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444

23  UIKit                               0x000000010a9fb87e __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 420

24  UIKit                               0x000000010abf8e8e _performActionsWithDelayForTransitionContext + 100

25  UIKit                               0x000000010a9fb67a -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231

26  UIKit                               0x000000010a777e9d -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392

27  UIKit                               0x0000000109fdefd2 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523

28  UIKit                               0x000000010a5b389b -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369

29  FrontBoardServices                  0x00000001145761b4 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338

30  FrontBoardServices                  0x000000011457eca9 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 235

31  libdispatch.dylib                   0x000000010e60243c _dispatch_client_callout + 8
32  libdispatch.dylib                   0x000000010e607af4 _dispatch_block_invoke_direct + 592
33  FrontBoardServices                  0x00000001145aa6ce __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
34  FrontBoardServices                  0x00000001145aa384 -[FBSSerialQueue _performNext] + 464
35  FrontBoardServices                  0x00000001145aa8f3 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
36  CoreFoundation                      0x000000010d5502b1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
37  CoreFoundation                      0x000000010d5efd31 __CFRunLoopDoSource0 + 81
38  CoreFoundation                      0x000000010d534c19 __CFRunLoopDoSources0 + 185
39  CoreFoundation                      0x000000010d5341ff __CFRunLoopRun + 1279
40  CoreFoundation                      0x000000010d533a89 CFRunLoopRunSpecific + 409
41  GraphicsServices                    0x00000001116e79c6 GSEventRunModal + 62
42  UIKit                               0x0000000109fe223c UIApplicationMain + 159
43  DoughNationAppBeta                  0x000000010886a557 main + 55
44  libdyld.dylib                       0x000000010e67ed81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type    NSException

问题是 @IBOutlet weak var searchbar: UISearchBar!@IBOutlet weak var SearchBar: UISearchBar!之前。 因此,当您在 Interface Builder 中附加搜索栏的 IBoutlet 时,属性 的名称是 SearchBar。之后,您在代码中更改了 属性 的名称,现在您需要打开界面生成器并与 iboutlet 建立新连接。这将解决您的问题。