首页 文章

xcode:class不符合键值编码[duplicate]

提问于
浏览
-1

这个问题在这里已有答案:

我正在通过本教程自学: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 回答

  • 1

    使用以下步骤检查您与 @IBAction 的连接:

    在故事板中:

    在您的视图控制器中:

    Updated:

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

  • 2

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

    如何解决这个问题?跟着这些步骤 .

    • 打开故事板,单击视图控制器场景下方的视图控制器 .

    • 检查ViewController的所有插座 .

    • 删除创建的那些插座,但未连接到ViewController.swift实现文件 .

    让我更新相同的内容 .

相关问题