Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

iphone - objects conforming to nscoding will not writetofile

I want to write an array of Question objects to file, but somehow writeToFile isn't doing anything. A Question has an Owner and and an array of Answer objects. An Answer has an Owner as well. All three of them conform to the NSCoding protocol (as far as I know).

From the code below, result returns NO. No clue what I'm doing wrong, since I am implementing NSCoding for everything, right?

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

Relevant line of code
BOOL result = [questions writeToFile:@"Users/brunoscheele/Desktop/questions.plist" atomically:YES];

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

writeToFile:atomically: writes property list files, not serialised archives. Property list types can't be extended by implementing NSCoding, and as your objects aren't one of the types supported by property lists, then they can't be written to a property list.

To archive your objects, you'll need to use an NSAchiver, for example:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...