我挡住了一些东西,我确信它太大了。我有一个看起来像这样的自定义对象
@interface DownloadObject : NSObject <NSCoding>{
NSNumber *key;
NSString *name;
NSNumber *progress;
NSNumber *progressBytes;
NSNumber *size;
NSString *path;
}
@property (copy) NSNumber *key;
@property (copy) NSString *name;
@property (copy) NSNumber *progress;
@property (copy) NSNumber *size;
@property (copy) NSString *path;
@property (copy) NSNumber *progressBytes;
-(id)initWithKey:(NSNumber *)k name:(NSString *)n progress:(NSNumber *)pro size:(NSNumber *)s path:(NSString *)p progressBytes:(NSNumber *)pb;
@end
以及实施
@implementation DownloadObject
@synthesize size, progress, name, key, path, progressBytes;
-(id)initWithKey:(NSNumber *)k name:(NSString *)n progress:(NSNumber *)pro size:(NSNumber *)s path:(NSString *)p progressBytes:(NSNumber *)pb
{
self.key = k;
self.name = n;
self.progress = pro;
self.size = s;
self.path = p;
self.progressBytes = pb;
return self;
}
-(id) initWithCoder: (NSCoder*) coder {
if (self = [super init]) {
self.key = [[coder decodeObjectForKey:@"Key"] retain];
self.name = [[coder decodeObjectForKey:@"Name"] retain];
self.progress = [[coder decodeObjectForKey:@"Progress"] retain];
self.size = [[coder decodeObjectForKey:@"Size"] retain];
self.path = [[coder decodeObjectForKey:@"Path"] retain];
self.progressBytes = [[coder decodeObjectForKey:@"ProgressBytes"]retain];
}
return self;
}
-(void) encodeWithCoder: (NSCoder*) coder {
[coder encodeObject:self.key forKey:@"Key"];
[coder encodeObject:self.name forKey:@"Name"];
[coder encodeObject:self.progress forKey:@"Progress"];
[coder encodeObject:self.size forKey:@"Size"];
[coder encodeObject:self.path forKey:@"Path"];
[coder encodeObject:self.progressBytes forKey:@"ProgressBytes"];
}
-(void)dealloc
{
[key release];
[name release];
[size release];
[progress release];
[path release];
[progressBytes release];
[super dealloc];
}
@end
如您所见,它实现了 NSCoding(我认为是,NSObject 不符合 NSCoding)。现在,当我尝试做类似的事情只是为了测试
downloadArray = [[[NSMutableArray alloc]init]retain];
NSNumber *number = [NSNumber numberWithInt:10];
DownloadObject *object = [[DownloadObject alloc]initWithKey:number name:@"hey" progress:number size:number path:@"hey" progressBytes:number];
[downloadArray addObject:object];
[object release];
[downloadArray writeToFile:path atomically:YES];
downloadArray
是一个NSMutableArray
。我的 plist 读/写很好,path
位于应用程序支持中,当我登录时显示 plist 路径。
但它只是不将数组写入 plist,知道吗?