简体   繁体   English

符合nscoding的对象不会写入文件

[英]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. 我想写一个Question对象数组到文件,但不知何故writeToFile没有做任何事情。 A Question has an Owner and and an array of Answer objects. 一个问题有一个所有者和一组Answer对象。 An Answer has an Owner as well. 答案也有所有者。 All three of them conform to the NSCoding protocol (as far as I know). 所有这三个都符合NSCoding协议(据我所知)。

From the code below, result returns NO. 从下面的代码中,结果返回NO。 No clue what I'm doing wrong, since I am implementing NSCoding for everything, right? 不知道我做错了什么,因为我正在为所有事情实施NSCoding,对吧?

Question.h 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 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 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 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 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 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];

writeToFile:atomically: writes property list files, not serialised archives. writeToFile:atomically:写入属性列表文件,而不是序列化归档。 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. 无法通过实现NSCoding来扩展属性列表类型,并且由于您的对象不是属性列表支持的类型之一,因此无法将它们写入属性列表。

To archive your objects, you'll need to use an NSAchiver , for example: 要归档对象,您需要使用NSAchiver ,例如:

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

Taken from here NSMutableArray writeToFile:atomically always returns NO on device but works fine on simulator 从这里采取NSMutableArray writeToFile:atomically总是在设备上返回NO但在模拟器上工作正常

I don't think you can not write into the application's bundle so you can not write to a plist in the Resources directory. 我不认为你不能写入应用程序的包,所以你不能写入Resources目录中的plist。 One good way is to copy the plist from the Resource directory into the Documents directory on first launch and access it from there in the future.. 一个好方法是在第一次启动时将plist从Resource目录复制到Documents目录,并在将来从那里访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM