简体   繁体   English

iOS-NSUserDefaults值为'valueForKey'返回null,但不为null

[英]iOS - NSUserDefaults value returns null for 'valueForKey' but isn't null

I am trying to see if a user has made a purchase by retrieving the NSUserDefaults value for each product identifier. 我试图通过检索每个产品标识符的NSUserDefaults值来查看用户是否购买了商品。 I save the value when a product is purchased and it is getting set correctly. 我在购买产品并正确设置它时保存值。

The key-value pair show up correctly when calling this: 调用此键/值对时可以正确显示:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

As this: 因此:

"com.COMPANY.PRODUCTIDENTIFIER" = 1;

But when I try to actually validate that specific key-value it always returns null: 但是,当我尝试实际验证该特定键值时,它总是返回null:

    for (NSString * productIdentifier in _productIdentifiers) {
        NSLog(@"ProductIdentifier: %@",productIdentifier);
        NSLog(@"Defaults Value: %@",[[NSUserDefaults standardUserDefaults] valueForKey:productIdentifier]);
        NSLog(@"Defaults Bool: %d",[[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier]);
        NSLog(@"Defaults Object: %@",[[NSUserDefaults standardUserDefaults] objectForKey:productIdentifier]);
        BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
        if (productPurchased) {
            [_purchasedProductIdentifiers addObject:productIdentifier];
            NSLog(@"Previously purchased: %@", productIdentifier);
        } else {
            NSLog(@"Not purchased: %@", productIdentifier);
        }
    }

Results in this: 结果:

2013-03-10 17:45:13.609 COMPANY[1140:907] ProductIdentifier:  com.COMPANY.PRODUCTIDENTIFIER
2013-03-10 17:45:13.611 COMPANY[1140:907] Defaults Value: (null)
2013-03-10 17:45:13.612 COMPANY[1140:907] Defaults Bool: 0
2013-03-10 17:45:13.611 COMPANY[1140:907] Defaults Object: (null)   

NOTE - The above methods are both called in the same method in this order, so it isn't a problem with the app not loading the defaults yet or anything like that since the value is correctly shown through the 'dictionaryRepresentation' method. 注意-上面的方法都以相同的顺序按相同的方法调用,因此应用程序不加载默认值或类似的东西就没有问题,因为通过'dictionaryRepresentation'方法正确显示了该值。 Also, Company and Product Identifiers have been replaced for privacy. 此外,出于隐私保护的目的,公司和产品标识符已被替换。 There are no typos in the real app so that is not the issue. 实际应用中没有错别字,所以这不是问题。

UPDATE 更新

Ok so this is interesting. 好的,这很有趣。 If I use a static string as the key, it returns the correct value. 如果我使用静态字符串作为键,它将返回正确的值。 But using the 'productIdentifier' (which is NSString) does not, even though logging the 'productIdentifier' it is the exact same string... Any Ideas??? 但是使用'productIdentifier'(NSString)不会,即使记录'productIdentifier'也是完全相同的字符串...任何想法?

Code: 码:

    for (NSString *productIdentifier in _productIdentifiers) {
        NSLog(@"ProductIdentifier: %@",productIdentifier);
        NSLog(@"Defaults Value: %@",[[NSUserDefaults standardUserDefaults] valueForKey:productIdentifier]);
        NSLog(@"Defaults Bool: %d",[[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier]);
        NSLog(@"Defaults Object: %@",[[NSUserDefaults standardUserDefaults] objectForKey:productIdentifier]);
        NSLog(@"Static Key: %d",[[NSUserDefaults standardUserDefaults] boolForKey:@"com.COMPANY.PRODUCTIDENTIFIER"]);
        BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
        if (productPurchased) {
            [_purchasedProductIdentifiers addObject:productIdentifier];
            NSLog(@"Previously purchased: %@", productIdentifier);
        } else {
            NSLog(@"Not purchased: %@", productIdentifier);
        }
    }

Result: 结果:

2013-03-10 17:45:13.609 COMPANY[1140:907] ProductIdentifier:  com.COMPANY.PRODUCTIDENTIFIER
2013-03-10 17:45:13.611 COMPANY[1140:907] Defaults Value: (null)
2013-03-10 17:45:13.612 COMPANY[1140:907] Defaults Bool: 0
2013-03-10 17:45:13.611 COMPANY[1140:907] Defaults Object: (null)   
2013-03-10 17:45:13.611 COMPANY[1140:907] Static Key: 1   

The problem is your use of valueForKey: instead of objectForKey: . 问题是您使用valueForKey:而不是objectForKey: valueForKey has different semantics (key-value coding), especially when used with a key that has dots in it. valueForKey具有不同的语义(键值编码),尤其是与带有点的键一起使用时。

Change this: 更改此:

[[NSUserDefaults standardUserDefaults] valueForKey:productIdentifier]

to this: 对此:

[[NSUserDefaults standardUserDefaults] objectForKey:productIdentifier]

and it should work. 它应该工作。

问题在于正在使用的NSSet中的产品标识符具有前导空格,因此它从来没有直接匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM