Attempt to set a non-property-list object as an NSUserDefaults

当使用NSUserDefault直接存储Facebook的token对象时,会报错(Attempt to set a non-property-list object as an NSUserDefaults),因为NSUserDefault无法直接存储自定义对象。


解决方法为:

1、存储对象

[cpp]  view plain copy
  1. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  2. NSData *tokenObject = [NSKeyedArchiver archivedDataWithRootObject:token];  
  3. [userDefaults setObject:tokenObject forKey:@"FBToken"]; 
  4.  [userDefaults synchronize];   


2、读取对象

[cpp]  view plain copy
  1. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];  
  2. NSData *tokenObject = [userDefaults objectForKey:@"FBToken"];  
  3. FBSDKAccessToken * token = [NSKeyedUnarchiver unarchiveObjectWithData:tokenObject];  


参考资料:

http://stackoverflow.com/questions/19720611/attempt-to-set-a-non-property-list-object-as-an-nsuserdefaults

http://blog.csdn.net/chyroger/article/details/5785297(实现原理以及如何实现存储自定义对象

你可能感兴趣的:(NSUserDefault)