iphone - NSUserDefaults 无法保存 NSMutableDictionary
<p><p>我正在尝试使用此代码将 NSMutableDictionary 保存到 NSUserDefaults:</p>
<pre><code>NSUserDefaults *def = ;
NSMutableDictionary *repositoryData = [ init];
;
;
;
;
;
</code></pre>
<p>但是我收到了这个消息:</p>
<pre><code> : Attempt to insert non-property value '{
persondata = "<Person: 0x1ed57e70>";
}' of class '__NSDictionaryM'.Note that dictionaries and arrays in property lists must also contain only property values.
</code></pre>
<p>而且 Person 类也有 NSCoding:</p>
<p>人.h</p>
<pre><code> @interface Person : NSObject <NSCoding> {
NSString *name;
NSString *email;
NSString *phoneNumber;
NSString *gender;
int age;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *phoneNumber;
@property (nonatomic, retain) NSString *gender;
@property (assign) int age;
@end
</code></pre>
<p>人.m</p>
<pre><code> @implementation Person
@synthesize name,email,phoneNumber,age,gender;
#pragma mark
#pragma mark NSCoder
- (void)encodeWithCoder:(NSCoder *)encoder{
;
;
;
;
;
}
- (id)initWithCoder:(NSCoder *)decoder {
if(self = ){
self.name = ;
self.email = ;
self.phoneNumber = ;
self.gender = ;
self.age = ;
}
return self;
}
@end
</code></pre>
<p>知道为什么会这样吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>对于什么类型的数据可以同步到 <code>NSDictionary</code> 中的 <code>NSUserDefaults</code> 有非常具体的限制。这些包括 <code>NSString、NSNumber、NSData、NSArray 和 NSDictionary</code>。在存储到 <code>NSUserDefaults</code> 之前,使用您的 <code>NSCoder</code> 方法序列化为 <code>NSData</code>(使用 <code>NSKeyedArchiver</code>)。</p>
<p>另外,看看 <a href="https://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults" rel="noreferrer noopener nofollow">this</a> </p></p>
<p style="font-size: 20px;">关于iphone - NSUserDefaults 无法保存 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14282892/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14282892/
</a>
</p>
页:
[1]