在registerUserDefaults之前工作后,NSUserDefaults返回null

时间:2016-03-07 04:37:11

标签: ios testing nsuserdefaults

我的代码以前工作过,现在它没有出于某种原因。我在设备上进行测试,然后删除了应用并使用<md-option ng-repeat="impact in impacts.availableImpacts" ng-value="impact.id">{{ impact.name }}</md-option> 进行了测试。现在默认值无法注册。

Test Flight

日志显示

  

appDefaults {       0 = AdShow;       50 = DidBuyInAppPurchase;   }

这样字典就可以了。然后记录密钥:

  

用户默认广告节目(null)

2 个答案:

答案 0 :(得分:1)

您使用了NSNumber numberWithInt,因此您需要使用

来检索int
[NSUserDefaults integerForKey]

像,

NSLog(@"user default ad show %ld", (long)[[NSUserDefaults standardUserDefaults] integerForKey:@"AdShow"]);

编辑1: - 您可以使用3选项

存储和检索它

选项1: - 如果您想将对象对象依据一起保存在 Nsuserdefault 中的注册类,那么请使用格式低于,

NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:@"50",@"DidBuyInAppPurchase",@"0",@"AdShow", nil];

[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"DidBuyInAppPurchase"]);
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"AdShow"]);

输出图像: -

enter image description here

选项2: - 将整个字典存储在NSUserDefaults中并在Dictionary中检索它。

NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:50],@"DidBuyInAppPurchase",[NSNumber numberWithInt:0],@"adshow", nil];
NSLog(@"appDefaults %@", dic);
[[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"appdeaults"];
[[NSUserDefaults standardUserDefaults] synchronize];

NSLog(@"user default ad show %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appdeaults"]);

在词典中检索

 NSMutableDictionary *mutableRetrievedDictionary = [[[NSUserDefaults standardUserDefaults] objectForKey:@"appdeaults"] mutableCopy];

 NSLog(@"==Dict %@",mutableRetrievedDictionary);

选项3: - 使用个人密钥的最后和简易存储int。

 // Store number with separate key
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:10] forKey:@"saveint10"];
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:15] forKey:@"saveint15"];

   //Retrieve it like ,
    NSInteger int1 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"saveint10"] intValue];
    NSInteger int2 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"saveint15"] intValue];
    NSLog(@"==int1 is %ld",(long)int1);
    NSLog(@"==int2 is %ld",(long)int2);

我希望这些信息对您有用。

答案 1 :(得分:0)

您可以单独设置值:

inet_pton