Xcode:类不符合键值编码。

3
我目前正在学习这个教程: 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) 

这是我的代码:

屏幕截图

还有按钮的连接检查器:

输入图像描述


你是否正确添加了标签和按钮的IBAction和IBOutlets? - Uma Madhavi
1
在连接插座后,您是否更改了方法的名称? - David Pilkington
3
我猜想你最初创建了一个名为 setDefaultLabelText 的 @IBOutlet,然后在代码中将其删除。然后你创建了同名的 @IBAction。故事板仍然连接到你创建的那个 @IBOutlet。在故事板中选择视图控制器,然后打开“连接检查器”,通过单击旁边的 x 删除名为 setDefaultLabelText 的 outlet。 - vacawama
1
哦,好的,是的,我最初是无意中创建了那个。不知道我必须以那种方式删除它。谢谢。 - Ginso89
@Ginso89,请检查更新的unsver。 - biloshkurskyi.ss
2个回答

3

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

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

  1. 打开您的故事板,在视图控制器场景下方单击视图控制器。
  2. 检查所有视图控制器的插座。
  3. 删除那些已创建但未连接到ViewController.swift实现文件的插座。

请保持更新。


1

请按照以下步骤检查您与 @IBAction 的连接:

在 storyboard 中: enter image description here enter image description here

在您的视图控制器中:

enter image description here

更新:

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


@Ginso89,在Storyboard左侧点击您的ViewController,然后在连接检查器中您应该能够找到它。 - biloshkurskyi.ss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接