Ios 使用NSUserdefaults注销用户时会出现什么错误?

Ios 使用NSUserdefaults注销用户时会出现什么错误?,ios,objective-c,Ios,Objective C,我不明白为什么会出现此错误: 注销时,我称之为: -(void)logoutUser { NSLog(@"Logging out user"); NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain]; } 在加载HomeCollection

我不明白为什么会出现此错误:

注销时,我称之为:

-(void)logoutUser {
    NSLog(@"Logging out user");
    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
}
在加载HomeCollectionViewController之前,我调用此命令检查包装它的导航控制器中是否存在NSUserDefaults:

-(void)viewDidAppear:(BOOL)animated {

    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


    //if values exist then already logged in else take to login screen
    if([[[defaults dictionaryRepresentation] allKeys] containsObject:@"userId"]){

        NSLog(@"userId found");
    } else {
        NSLog(@"userId NOT found");
        StarterViewController *hc=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Starter"];
        [self presentViewController:hc animated:YES completion:nil];

    }

}
这就是错误:

 2014-04-28 10:19:38.431 App[2675:60b] Warning: Attempt to present <HomeCollectionViewController: 0xba9dd10> on <SlidingMenuViewController: 0xba94200> whose view is not in the window hierarchy!
2014-04-28 10:19:38.443 App[2675:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: value parameter is nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x01dff1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01b7e8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01dfefbb +[NSException raise:format:] + 139
    3   Foundation                          0x018c4a42 +[NSJSONSerialization dataWithJSONObject:options:error:] + 92
    4   Stand                               0x00008769 -[HomeCollectionViewController getHomeData] + 297
    5   Stand                               0x000084a8 -[HomeCollectionViewController viewDidLoad] + 744
    6   UIKit                               0x0095d33d -[UIViewController loadViewIfRequired] + 696
    7   UIKit                               0x0095d5d9 -[UIViewController view] + 35
    8   UIKit                               0x00988509 -[UINavigationController rotatingSnapshotViewForWindow:] + 52
    9   UIKit                               0x00ccfe3c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 420
    10  UIKit                               0x00884c22 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 1495
    11  UIKit                               0x00884646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    12  UIKit                               0x00884518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    13  UIKit                               0x008845a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    14  UIKit                               0x0088363a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    15  UIKit                               0x0088359c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    16  UIKit                               0x008842f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    17  UIKit                               0x008878e6 -[UIWindow setDelegate:] + 449
    18  UIKit                               0x00961b77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    19  UIKit                               0x0087d474 -[UIWindow addRootViewControllerViewIfPossible] + 591
    20  UIKit                               0x0087d5ef -[UIWindow _setHidden:forced:] + 312
    21  UIKit                               0x0087d86b -[UIWindow _orderFrontWithoutMakingKey] + 49
    22  UIKit                               0x008883c8 -[UIWindow makeKeyAndVisible] + 65
    23  UIKit                               0x00838bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    24  UIKit                               0x0083d667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    25  UIKit                               0x00851f92 -[UIApplication handleEvent:withNewEvent:] + 3517
    26  UIKit                               0x00852555 -[UIApplication sendEvent:] + 85
    27  UIKit                               0x0083f250 _UIApplicationHandleEvent + 683
    28  GraphicsServices                    0x03bbff02 _PurpleEventCallback + 776
    29  GraphicsServices                    0x03bbfa0d PurpleEventCallback + 46
    30  CoreFoundation                      0x01d7aca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    31  CoreFoundation                      0x01d7a9db __CFRunLoopDoSource1 + 523
    32  CoreFoundation                      0x01da568c __CFRunLoopRun + 2156
    33  CoreFoundation                      0x01da49d3 CFRunLoopRunSpecific + 467
    34  CoreFoundation                      0x01da47eb CFRunLoopRunInMode + 123
    35  UIKit                               0x0083cd9c -[UIApplication _run] + 840
    36  UIKit                               0x0083ef9b UIApplicationMain + 1225
    37  Stand                               0x0000948d main + 141
    38  libdyld.dylib                       0x0254b701 start + 1
    39  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
2014-04-28 10:19:38.431应用程序[2675:60b]警告:试图在不在窗口层次结构中的视图上显示!
2014-04-28 10:19:38.443应用程序[2675:60b]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“***+[NSJSONSerialization dataWithJSONObject:选项:错误:]:值参数为零”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x01dff1e4例外预处理+180
1 libobjc.A.dylib 0x01b7e8e5 objc_异常_抛出+44
2 CoreFoundation 0x01dfefbb+[N异常提升:格式:][139
3基金会0x018C4A42+[ NSJSONSONTRORATION DATAWORJSONObj:选项:错误:] + 92
4个支架0x00008769-[HomeCollectionViewController getHomeData]+297
5个支架0x000084a8-[HomeCollectionViewController viewDidLoad]+744
6 UIKit 0x0095d33d-[UIViewController loadViewIfRequired]+696
7 UIKit 0x0095d5d9-[UIViewController视图]+35
8 UIKit 0x00988509-[UINavigationController旋转快照视图用于窗口:+52
9 UIKit 0x00ccfe3c-[UIClientRotationContext initWithClient:TooOrientation:duration:andWindow:+420
10 UIKit 0x00884c22-[UIWindow\u setRotatableClient:TooOrientation:updateStatusBar:duration:force:isRotating:+1495
11 UIKit 0x00884646-[UIWindow\u setRotatableClient:TooOrientation:updateStatusBar:duration:force:+82
12 UIKit 0x00884518-[UIWindow\u setRotatableViewOrientation:updateStatusBar:duration:force:+117
13 UIKit 0x008845a0-[UIWindow\u setRotatableViewOrientation:持续时间:强制:+67
14 UIKit 0x0088363a uu 57-[UIWindow updateToInterfaceOrientation:duration:force:][uBlock_invoke+120
15 UIKit 0x0088359c-[UIWindow\u updateToInterfaceOrientation:duration:force:+400
16 UIKit 0x008842f3-[UIWindow setAutorotates:forceUpdateInterfaceOrientation:+870
17 UIKit 0x008878e6-[UIWindow setDelegate:+449
18 UIKit 0x00961b77-[UIViewController\u TryBecomerootViewController窗口:+180
19 UIKit 0x0087d474-[UIWindow AddRootViewControllerServiceWifPossible]+591
20 UIKit 0x0087d5ef-[UIWindow\u setHidden:强制:][312
21 UIKit 0x0087d86b-[UIWindow\u OrderFront不带制作键]+49
22 UIKit 0x008883c8-[UIWindow makeKeyAndVisible]+65
23 UIKit 0x00838bc0-[UIApplication\u CallInitializationDelegatesForRL:有效负载:挂起:][2097
24 UIKit 0x0083d667-[UIApplication\u运行时URL:payload:launchOrientation:statusBarStyle:statusBarHidden:+824
25 UIKit 0x00851f92-[UIApplication handleEvent:withNewEvent:][3517
26 UIKit 0x00852555-[UIApplication sendEvent:+85
27 UIKit 0x0083f250 _UIApplicationHandleEvent+683
28图形服务0x03bbff02 _PurpleEventCallback+776
29图形服务0x03bbfa0d PurpleEventCallback+46
30 CoreFoundation 0x01d7aca5\uuuu CFRUNLOOP\u正在调用\uu OUT\uu以执行\uu功能\uuuu+53
31 CoreFoundation 0x01d7a9db\uu CFRunLoopDoSource1+523
32 CoreFoundation 0x01da568c__CFRunLoopRun+2156
33 CoreFoundation 0x01da49d3 CFRunLoopRunSpecific+467
34 CoreFoundation 0x01da47eb CFRUNLOOPSRUNINMODE+123
35 UIKit 0x0083cd9c-[UIU应用程序运行]+840
36 UIKit 0x0083ef9b UIApplicationMain+1225
37支架0x0000948d干管+141
38 libdyld.dylib 0x0254b701开始+1
39°?0x00000001 0x0+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止

我认为您对问题原因的假设不正确。正在加载您的
HomeCollectionViewController
,因此您的用户默认值不是问题所在。您可以从堆栈跟踪中看到,此视图控制器正在其
viewDidLoad
方法中调用方法
getHomeData
。似乎
getHomeData
正在尝试检索一些JSON数据,但JSON字符串为nil-根据异常消息


您需要研究此方法并找出JSON的问题所在。

我不确定您显示的代码是否与stacktrace.bz相关,您得到的值为nil问题是,如果方法ViewDidDisplay出现,则根本不应调用HomeCollectionViewController(该方法在包装它的导航控制器中被调用)结果是什么?或者我需要补充什么吗?我认为你对你的问题得出了错误的结论。正在调用HomeCollectionViewController-从堆栈跟踪可以看出这一点。此视图控制器正在其
viewDidLoad
中调用方法
getHomeData
。似乎
getHomeData
正在尝试重新设置