iOS - NSUserDefaults value returns null for 'v

2019-08-31 04:14发布

问题:

I am trying to see if a user has made a purchase by retrieving the NSUserDefaults value for each product identifier. 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:

    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. 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???

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   

回答1:

The problem is your use of valueForKey: instead of objectForKey:. valueForKey has different semantics (key-value coding), especially when used with a key that has dots in it.

Change this:

[[NSUserDefaults standardUserDefaults] valueForKey:productIdentifier]

to this:

[[NSUserDefaults standardUserDefaults] objectForKey:productIdentifier]

and it should work.



回答2:

Problem was the product identifier in the NSSet being used had a leading whitespace so it was never a direct match.