xcode:class不符合键值编码

时间:2017-12-08 12:53:21

标签: ios swift xcode iboutlet

我目前正在完成本教程: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ConnectTheUIToCode.html#//apple_ref/doc/uid/TP40015214-CH22-SW1 在为按钮创建函数后(在页面上的~2 / 3),我尝试运行代码并获得异常:

2017-12-08 11:24:31.343183+0100 FoodTracker[2159:154603] *** 

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FoodTracker.ViewController 0x7fea6f406820> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key setDefaultLabelText.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000109d7512b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00000001060fcf41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000109d75079 -[NSException raise] + 9
    3   Foundation                          0x0000000105b1ba63 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x0000000106b39117 -[UIViewController setValue:forKey:] + 87
    5   UIKit                               0x0000000106e2ac2d -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x0000000109d183cd -[NSArray makeObjectsPerformSelector:] + 317
    7   UIKit                               0x0000000106e295e3 -[UINib instantiateWithOwner:options:] + 1856
    8   UIKit                               0x0000000106b403ff -[UIViewController _loadViewFromNibNamed:bundle:] + 383
    9   UIKit                               0x0000000106b40d2b -[UIViewController loadView] + 177
    10  UIKit                               0x0000000106b4105c -[UIViewController loadViewIfRequired] + 195
    11  UIKit                               0x0000000106b418b9 -[UIViewController view] + 27
    12  UIKit                               0x0000000106a0c7cf -[UIWindow addRootViewControllerViewIfPossible] + 122
    13  UIKit                               0x0000000106a0ced7 -[UIWindow _setHidden:forced:] + 294
    14  UIKit                               0x0000000106a1fe54 -[UIWindow makeKeyAndVisible] + 42
    15  UIKit                               0x00000001069928b8 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4737
    16  UIKit                               0x0000000106997aeb -[UIApplication _runWithMainScene:transitionContext:completion:] + 1720
    17  UIKit                               0x0000000106d616f8 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 924
    18  UIKit                               0x00000001071374c8 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    19  UIKit                               0x0000000106d612f1 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249
    20  UIKit                               0x0000000106d61b6b -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 696
    21  UIKit                               0x00000001076dfa69 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 262
    22  UIKit                               0x00000001076df922 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444
    23  UIKit                               0x00000001073bc9c8 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    24  UIKit                               0x00000001075bbb06 _performActionsWithDelayForTransitionContext + 100
    25  UIKit                               0x00000001073bc88b -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231
    26  UIKit                               0x0000000107136b25 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    27  UIKit                               0x000000010699636a -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523
    28  UIKit                               0x0000000106f71605 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369
    29  FrontBoardServices                  0x000000010bc38cc0 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338
    30  FrontBoardServices                  0x000000010bc417b5 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 235
    31  libdispatch.dylib                   0x000000010ae9733d _dispatch_client_callout + 8
    32  libdispatch.dylib                   0x000000010ae9c9f3 _dispatch_block_invoke_direct + 592
    33  FrontBoardServices                  0x000000010bc6d498 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    34  FrontBoardServices                  0x000000010bc6d14e -[FBSSerialQueue _performNext] + 464
    35  FrontBoardServices                  0x000000010bc6d6bd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    36  CoreFoundation                      0x0000000109d18101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    37  CoreFoundation                      0x0000000109db7f71 __CFRunLoopDoSource0 + 81
    38  CoreFoundation                      0x0000000109cfca19 __CFRunLoopDoSources0 + 185
    39  CoreFoundation                      0x0000000109cfbfff __CFRunLoopRun + 1279
    40  CoreFoundation                      0x0000000109cfb889 CFRunLoopRunSpecific + 409
    41  GraphicsServices                    0x000000010c5019c6 GSEventRunModal + 62
    42  UIKit                               0x00000001069995d6 UIApplicationMain + 159
    43  FoodTracker                         0x00000001057e7b67 main + 55
    44  libdyld.dylib                       0x000000010af13d81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

这是我的代码:

Screenshot

按钮的连接检查器:

enter image description here

2 个答案:

答案 0 :(得分:3)

这是因为故事板中的FoodTracker VC创建了一个插座但未连接。

如何解决问题?请按照以下步骤操作。

  1. 打开故事板,单击视图控制器场景下方的视图控制器。
  2. 检查ViewController的所有插座。
  3. 删除创建的插座,但未连接到ViewController.swift实现文件。
  4. 让我更新相同内容。

答案 1 :(得分:1)

使用以下步骤检查您与@IBAction的关联:

在故事板中: enter image description here enter image description here

在视图控制器中:

enter image description here

<强>更新

在您的情况下,您应该删除Reference Outlets中的连接,因为您将其连接到标签插座。