3

我想将一组 Question 对象写入文件,但不知何故 writeToFile 没有做任何事情。Question 有一个 Owner 和一个 Answer 对象数组。答案也有所有者。他们三个都符合 NSCoding 协议(据我所知)。

从下面的代码中,结果返回 NO。不知道我做错了什么,因为我正在为所有事情实施 NSCoding,对吗?

问题.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

问题.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

答案.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

答案.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

所有者.h

#import <Foundation/Foundation.h>


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

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

@end

所有者.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];

4

2 回答 2

10

writeToFile:atomically:写入属性列表文件,而不是序列化档案。属性列表类型不能通过实现来扩展NSCoding,并且由于您的对象不是属性列表支持的类型之一,因此它们不能写入属性列表。

要归档您的对象,您需要使用NSAchiver,例如:

[NSKeyedArchiver archiveRootObject:questions 
                 toFile:@"some/path/questions.archive"]
于 2011-03-18T12:44:27.567 回答
0

取自这里 NSMutableArray writeToFile:atomically 总是在设备上返回 NO 但在模拟器上工作正常

我不认为您不能写入应用程序的包,因此您不能写入资源目录中的 plist。一个好方法是在第一次启动时将 plist 从 Resource 目录复制到 Documents 目录中,并在将来从那里访问它。

于 2011-03-18T12:43:25.940 回答