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

Iphone 符合nscoding的对象将不会写入文件,iphone,objective-c,nscoding,Iphone,Objective C,Nscoding,我想将一组问题对象写入文件,但不知何故writeToFile什么也没做。问题具有所有者和答案对象数组。答案也有主人。这三个都符合NSCoding协议(据我所知) 从下面的代码中,结果返回NO。不知道我做错了什么,因为我正在为所有内容实现NSCoding,对吗 问题.h #import <Foundation/Foundation.h> #import "Owner.h" @interface Question : NSObject <NSCoding> { NS

我想将一组问题对象写入文件,但不知何故writeToFile什么也没做。问题具有所有者和答案对象数组。答案也有主人。这三个都符合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
答案.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
#import <Foundation/Foundation.h>


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

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

@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
#import <Foundation/Foundation.h>


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

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

@end
相关代码行
BOOL result=[questions writeToFile:@“Users/brunoscheele/Desktop/questions.plist”原子级:YES]

取自此处


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

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

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

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

我不完全确定序列化存档是什么意思,所以我不确定为什么对象数组不符合条件。然而,这是可行的,所以谢谢:)我也被术语“序列化档案”弄糊涂了;这是指包含指向其他对象的指针的NSMutableArray吗?
[NSKeyedArchiver archiveRootObject:questions 
                 toFile:@"some/path/questions.archive"]