1
我無法獲得archivedDataWithRootObject方法來調用encodeWithCoder,它看起來很簡單,從我的理解,但它不工作。 下面的代碼:NSKeyedArchiver archivedDataWithRootObject:不會調用encodeWithCoder
#import <Foundation/Foundation.h>
@interface Details : NSObject <NSCoding>{
NSMutableArray *myArray;
}
@property(nonatomic,retain) NSMutableArray *myArray;
@end
#import "Details.h"
@implementation Details
@synthesize myArray;
-(id)init{
[super init];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
NSLog(@"initWithCoder");
if (self = [super init])
{
self.myArray = [[aDecoder decodeObjectForKey:@"details"]retain];
}
return self;
}
- (NSMutableArray*)getDetails{
NSLog(@"getDetials");
// NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *details = [[NSUserDefaults standardUserDefaults] objectForKey:@"details"];
if (details != nil){
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:details];
if (oldSavedArray != nil)
self.myArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
self.myArray = [[NSMutableArray alloc] initWithCapacity:1];
}
return self.myArray;
}
- (void) addDetails:(NSMutableArray *)details{
NSLog(@"add Details");
[self.myArray addObject:details];
[NSKeyedArchiver archivedDataWithRootObject:self.myArray];
}
- (void) encodeWithCoder:(NSCoder *)coder;
{
NSLog(@"encodeWithCoder");
[coder encodeObject:self.myArray forKey:@"details"];
}
@end
的NSLog(@ 「encodeWithCoder」)不運行。 我可能會錯過一些東西,如果有人有任何想法,將不勝感激。
謝謝你這麼多jackslash,我想我現在明白了。至少它現在調用這個方法,其餘的都是Ill。真棒。 Brett – BrettStuart 2012-02-02 11:50:20
您可以使用[NSKeyedArchiver archiveRootObject:_rootObject toFile:_filePath]直接將文件直接歸檔到文件中; – Edwin 2012-06-21 18:56:40