2011-03-18 53 views
3

我想寫一個Question對象數組到文件,但不知何故writeToFile沒有做任何事情。一個問題有一個所有者和一個Answer對象數組。答案也有一個所有者。他們三個都符合NSCoding協議(據我所知)。符合nscoding的對象將不會寫入文件

從下面的代碼中,結果返回NO。不知道我做錯了什麼,因爲我正在爲所有事情實施NSCoding,對吧?

Question.h

#import <Foundation/Foundation.h> 
#import "Owner.h" 

@interface Question : NSObject <NSCoding> { 
    NSString *questionId; 
    NSString *questionRevision; 
    NSString *text; 
    NSDate *date; 
    NSMutableArray *answers; 
    NSString *page; 
    NSNumber *questionLocation; 

    Owner *owner; 
} 

@property (nonatomic, retain) NSString *questionId; 
@property (nonatomic, retain) NSString *questionRevision; 
@property (nonatomic, retain) NSString *text; 
@property (nonatomic, retain) NSDate *date; 
@property (nonatomic, retain) NSMutableArray *answers; 
@property (nonatomic, retain) NSString *page; 
@property (nonatomic, retain) NSNumber *questionLocation; 
@property (nonatomic, retain) Owner *owner; 

@end 

Question.m

#import "Question.h" 
#import "Answer.h" 

@implementation Question 

@synthesize questionId, questionRevision, text, date, answers, page, questionLocation, owner; 

- (void)encodeWithCoder:(NSCoder *)coder 
{ 
    //[super encodeWithCoder:coder]; 
    [coder encodeObject:questionId forKey:@"questionId"]; 
    [coder encodeObject:questionRevision forKey:@"questionRevision"]; 
    [coder encodeObject:text forKey:@"text"]; 
    [coder encodeObject:date forKey:@"date"]; 
    [coder encodeObject:answers forKey:@"answers"]; 
    [coder encodeObject:page forKey:@"page"]; 
    [coder encodeObject:questionLocation forKey:@"questionLocation"]; 
    [coder encodeObject:owner forKey:@"owner"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    //self = [super initWithCoder:decoder]; 
    self = [super init]; 
    self.questionId = [decoder decodeObjectForKey:@"questionId"]; 
    self.questionRevision = [decoder decodeObjectForKey:@"questionRevision"]; 
    self.text = [decoder decodeObjectForKey:@"text"]; 
    self.date = [decoder decodeObjectForKey:@"date"]; 
    self.answers = [decoder decodeObjectForKey:@"answers"]; 
    self.page = [decoder decodeObjectForKey:@"page"]; 
    self.questionLocation = [decoder decodeObjectForKey:@"questionLocation"]; 
    self.owner = [decoder decodeObjectForKey:@"owner"]; 
} 

@end 

Answer.h

#import <Foundation/Foundation.h> 
#import "Owner.h" 

@interface Answer : NSObject <NSCoding> { 
    Owner *owner; 
    NSString *text; 
    NSDate *date; 
} 

@property (nonatomic, retain) Owner *owner; 
@property (nonatomic, retain) NSString *text; 
@property (nonatomic, retain) NSDate *date; 

@end 

Answer.m

#import "Answer.h" 


@implementation Answer 

@synthesize owner, text, date; 

    - (void)encodeWithCoder:(NSCoder *)coder 
    { 
    //[super encodeWithCoder:coder]; 
    [coder encodeObject:owner forKey:@"owner"]; 
    [coder encodeObject:text forKey:@"text"]; 
    [coder encodeObject:date forKey:@"date"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    //self = [super initWithCoder:decoder]; 
    self = [super init]; 
    self.owner = [decoder decodeObjectForKey:@"owner"]; 
    self.text = [decoder decodeObjectForKey:@"text"]; 
    self.date = [decoder decodeObjectForKey:@"date"]; 
} 

@end 

Owner.h

#import <Foundation/Foundation.h> 


@interface Owner : NSObject <NSCoding> { 
    NSString *name; 
    NSString *photoFileName; 
} 

@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *photoFileName; 

@end 

Owner.m

#import "Owner.h" 


@implementation Owner 

@synthesize name, photoFileName; 

- (void)encodeWithCoder:(NSCoder *)coder 
{ 
    //[super encodeWithCoder:coder]; 
    [coder encodeObject:name forKey:@"name"]; 
    [coder encodeObject:photoFileName forKey:@"photoFileName"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder 
{ 
    //self = [super initWithCoder:decoder]; 
    self = [super init]; 
    self.name = [decoder decodeObjectForKey:@"name"]; 
    self.photoFileName = [decoder decodeObjectForKey:@"photoFileName"]; 
} 

@end 

相關的代碼線
BOOL result = [questions writeToFile:@"Users/brunoscheele/Desktop/questions.plist" atomically:YES];

回答

10

writeToFile:atomically:寫入屬性列表文件,而不是序列化存檔。無法通過實施NSCoding來擴展屬性列表類型,並且由於您的對象不是屬性列表支持的類型之一,因此無法將其寫入屬性列表。

要歸檔的對象,你需要使用一個NSAchiver,例如:

[NSKeyedArchiver archiveRootObject:questions 
       toFile:@"some/path/questions.archive"] 
+0

不完全知道你的序列化存檔的意思,所以我不知道爲什麼對象數組沒有資格。但是,這工作,所以謝謝:) – SpacyRicochet 2011-03-18 13:15:19

+0

我也被術語「序列化檔案」困惑;這是參考包括指向其他​​對象的NSMutableArray? – 2012-11-21 08:20:34

+0

啊好的 - http://petermcintyre.com/topics/intro-to-archiving/ – 2012-11-21 22:48:15