NSUnknownKeyException错误,我从哪里开始

时间:2018-01-27 11:43:58

标签: ios swift

最近开始学习swift,我收到一条错误消息,我不知道该在哪里。我正在尝试创建一个将用户电话号码存储在UserDefaults中的简单应用程序。我不确定我放置代码的位置或其他愚蠢的错误。提前感谢您的帮助。

查看控制器:

class ViewController: UIViewController {

    @IBOutlet var userNumberText: UITextField!
    @IBOutlet var blankView: UITextView!

    @IBAction func saveButton(_ sender: Any) {
    if(userNumberText.text?.count)! > 0 {
    let number = UserDefaults.standard.set(userNumberText.text, forKey: "number")
        }
    }



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
            let defaults = UserDefaults.standard
        if let number = defaults.string(forKey: "number"){
            blankView.text = number;
        }   
}
}

错误堆栈:

2018-01-27 06:36:23.863147-0500 Number Saver[1065:85326] [MC] Lazy loading NSBundle MobileCoreServices.framework
2018-01-27 06:36:23.864677-0500 Number Saver[1065:85326] [MC] Loaded MobileCoreServices.framework
2018-01-27 06:36:23.921929-0500 Number Saver[1065:85326] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Number_Saver.ViewController 0x7fa0f9f06280> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key blankText.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f9fa12b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010f08ef41 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f9fa079 -[NSException raise] + 9
    3   Foundation                          0x000000010eaada63 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292
    4   UIKit                               0x0000000110018117 -[UIViewController setValue:forKey:] + 87
    5   UIKit                               0x0000000110309c2d -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x000000010f99d3cd -[NSArray makeObjectsPerformSelector:] + 317
    7   UIKit                               0x00000001103085e3 -[UINib instantiateWithOwner:options:] + 1856
    8   UIKit                               0x000000011001f3ff -[UIViewController _loadViewFromNibNamed:bundle:] + 383
    9   UIKit                               0x000000011001fd2b -[UIViewController loadView] + 177
    10  UIKit                               0x000000011002005c -[UIViewController loadViewIfRequired] + 195
    11  UIKit                               0x00000001100208b9 -[UIViewController view] + 27
    12  UIKit                               0x000000010feeb7cf -[UIWindow addRootViewControllerViewIfPossible] + 122
    13  UIKit                               0x000000010feebed7 -[UIWindow _setHidden:forced:] + 294
    14  UIKit                               0x000000010fefee54 -[UIWindow makeKeyAndVisible] + 42
    15  UIKit                               0x000000010fe718b8 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4737
    16  UIKit                               0x000000010fe76aeb -[UIApplication _runWithMainScene:transitionContext:completion:] + 1720
    17  UIKit                               0x00000001102406f8 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 924
    18  UIKit                               0x00000001106164c8 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
    19  UIKit                               0x00000001102402f1 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249
    20  UIKit                               0x0000000110240b6b -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 696
    21  UIKit                               0x0000000110bbea69 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 262
    22  UIKit                               0x0000000110bbe922 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444
    23  UIKit                               0x000000011089b9c8 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
    24  UIKit                               0x0000000110a9ab06 _performActionsWithDelayForTransitionContext + 100
    25  UIKit                               0x000000011089b88b -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231
    26  UIKit                               0x0000000110615b25 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
    27  UIKit                               0x000000010fe7536a -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523
    28  UIKit                               0x0000000110450605 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369
    29  FrontBoardServices                  0x0000000114bcacc0 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338
    30  FrontBoardServices                  0x0000000114bd37b5 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 235
    31  libdispatch.dylib                   0x0000000113e2933d _dispatch_client_callout + 8
    32  libdispatch.dylib                   0x0000000113e2e9f3 _dispatch_block_invoke_direct + 592
    33  FrontBoardServices                  0x0000000114bff498 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    34  FrontBoardServices                  0x0000000114bff14e -[FBSSerialQueue _performNext] + 464
    35  FrontBoardServices                  0x0000000114bff6bd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    36  CoreFoundation                      0x000000010f99d101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    37  CoreFoundation                      0x000000010fa3cf71 __CFRunLoopDoSource0 + 81
    38  CoreFoundation                      0x000000010f981a19 __CFRunLoopDoSources0 + 185
    39  CoreFoundation                      0x000000010f980fff __CFRunLoopRun + 1279
    40  CoreFoundation                      0x000000010f980889 CFRunLoopRunSpecific + 409
    41  GraphicsServices                    0x00000001154939c6 GSEventRunModal + 62
    42  UIKit                               0x000000010fe785d6 UIApplicationMain + 159
    43  Number Saver                        0x000000010e779b57 main + 55
    44  libdyld.dylib                       0x0000000113ea5d81 start + 1
    45  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

0 个答案:

没有答案