objective-c - 无法将我的类(class)的 NSMutableArray 保存到文件(IOS)
<p><p>无法找出我的代码不起作用的原因。请帮助某人。</p>
<p>我创建了自己的类,实现了 NSCoding 协议(protocol)。不知道我错过了什么或错了什么。</p>
<p><strong>这里是保存代码</strong></p>
<pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDirectory = ;
NSString *fileName = ;
Item * item = [ init];
item.x = 3; item.y = 5; item.type = (TType) 3; item.isSelected = NO;
NSMutableArray * array = [ init];
;
// ( Doesn't Save the file ,returns NO);
</code></pre>
<p><strong>这是我类(class)的代码</strong>
*<em>.h</em>*</p>
<pre><code>#import <Foundation/Foundation.h>
enum TType
{
kNone = 0,
KFirst = 1,
....
};
@interface Item : NSObject <NSCoding>{
}
@property (nonatomic) int x;
@property (nonatomic) int y;
@property (nonatomic) enum TType type;
@property (nonatomic) BOOL isSelected;
@end
</code></pre>
<p><strong>.m</strong></p>
<pre><code>@implementation Item
@synthesize x, y , type , isSelected;
#pragma mark NSCoding Protocol
- (void)encodeWithCoder:(NSCoder *)encoder;
{
forKey:@"x"];
forKey:@"y"];
forKey:@"type"];
forKey:@"isSelected"];
}
- (id)initWithCoder:(NSCoder *)decoder;
{
if ( ! )
return nil;
];
];
];
];
return self;
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我想您会在以下位置找到答案:<a href="https://stackoverflow.com/questions/5352032/objects-conforming-to-nscoding-will-not-writetofile" rel="noreferrer noopener nofollow">objects conforming to nscoding will not writetofile</a> </p>
<p>即您不能将 <code>Item</code> 类序列化为属性列表,因为它不是属性列表对象(<code>NSString</code>、<code>NSData</code>、<code>NSArray</code> 或 <code>NSDictionary</code>)。</p>
<p>参见 <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/writeToFile:atomically:" rel="noreferrer noopener nofollow"><code>writeToFile:atomically:</code></a> 的文档:</p>
<blockquote>
<p>This method recursively validates that all the contained objects are property list objects before writing out the file, and returns <code>NO</code> if all the objects are not property list objects, since the resultant file would not be a valid property list.</p>
</blockquote></p>
<p style="font-size: 20px;">关于objective-c - 无法将我的类(class)的 NSMutableArray 保存到文件(IOS),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9756861/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9756861/
</a>
</p>
页:
[1]