要在NSUserDefaults中获取值,可以使用以下功能:
arrayForKey(_:) boolForKey(_:) dataForKey(_:) dictionaryForKey(_:) floatForKey(_:) integerForKey(_:) objectForKey(_:) stringArrayForKey(_:) stringForKey(_:) doubleForKey(_:) URLForKey(_:)
-(nullable NSArray *)arrayForKey:(nonnull NSString *)defaultName; -(BOOL)boolForKey:(nonnull NSString *)defaultName; -(nullable NSData *)dataForKey:(nonnull NSString *)defaultName; -(nullable NSDictionary<NSString *, id> *)dictionaryForKey:(nonnull NSString *)defaultName; -(float)floatForKey:(nonnull NSString *)defaultName; -(NSInteger)integerForKey:(nonnull NSString *)defaultName; -(nullable id)objectForKey:(nonnull NSString *)key; -(nullable NSArray<NSString *> *)stringArrayForKey:(nonnull NSString *)defaultName; -(nullable NSString *)stringForKey:(nonnull NSString *)defaultName; -(double)doubleForKey:(nonnull NSString *)defaultName; -(nullable NSURL *)URLForKey:(nonnull NSString *)defaultName;
用法示例为:
let homeCountry = NSUserDefaults.standardUserDefaults().stringForKey("HomeCountry")
NSString *homeCountry = [[NSUserDefaults standardUserDefaults] stringForKey:@"HomeCountry"];