NSKeyedArchiver 不保存对象的所有属性

发布于 2024-11-07 08:14:25 字数 1548 浏览 0 评论 0原文

我遇到一个问题,除其他外,我正在保存一个具有颜色和标签属性的“节点”数组(下面的标题)。当保存为 - (void)saveDataToDiskWithPath:(NSURL*)path 时,日志显示这些属性存在。当我稍后从保存的文件中再次加载数组时,节点的框架仍然存在,但数组中的每个节点都丢失了其标签和颜色......有什么想法吗?我可能做错了什么?也许我只是累了,稍后一切都会有意义...

Node.h:

//
//  Node.h
//

#import <Cocoa/Cocoa.h>


@interface Node : NSView {
@private
    NSColor *thisColor;
    NSColor *originalColor;
    NSRect dragRect;
    BOOL msDn;
    long tag;
    BOOL amSelected;
}

@property long tag;
@property (nonatomic, retain) NSColor* originalColor;

- (id)initWithFrame:(NSRect)frame andColor:(NSColor*)color;
- (void)select:(BOOL)yesOrNo;

@end

保存方法:

- (void) saveDataToDiskWithPath:(NSURL*)path
{
    NSLog(@"Path is %@", [path absoluteString]);
    NSMutableDictionary * rootObject;
    rootObject = [NSMutableDictionary dictionary];
    for(Node* node in [sharedValues nodes]){
        NSLog(@"Node has color %@ and tag %ld.", [node originalColor], [node tag]);
    }
    [rootObject setValue: [imageView aPath] forKey:@"path"];
    [rootObject setValue:[sharedValues nodes] forKey:@"nodes"];
    [NSKeyedArchiver archiveRootObject: rootObject toFile: [path path]];
}

加载方法:

- (void)loadDataFromDiskWithPath:(NSURL*)path
{
    NSDictionary * rootObject;

    rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[path path]];    
    [imageView setAPath:[rootObject valueForKey:@"path"]];
    [sharedValues setNodes:[rootObject valueForKey:@"nodes"]];
    [imageView setNeedsDisplay:YES];
}

I am having an issue where I am saving, among other things, an array of "nodes" (header below) that posses a color and a tag property. When it saves as in - (void)saveDataToDiskWithPath:(NSURL*)path, the log shows that these properties exist. When I load the array again at a later time from the saved file, the frame of the node persists, but each node in the array has lost its tag and color...any ideas why? What could I be doing wrong? Maybe I'm just tired, and it will all make sense later...

Node.h:

//
//  Node.h
//

#import <Cocoa/Cocoa.h>


@interface Node : NSView {
@private
    NSColor *thisColor;
    NSColor *originalColor;
    NSRect dragRect;
    BOOL msDn;
    long tag;
    BOOL amSelected;
}

@property long tag;
@property (nonatomic, retain) NSColor* originalColor;

- (id)initWithFrame:(NSRect)frame andColor:(NSColor*)color;
- (void)select:(BOOL)yesOrNo;

@end

The save method:

- (void) saveDataToDiskWithPath:(NSURL*)path
{
    NSLog(@"Path is %@", [path absoluteString]);
    NSMutableDictionary * rootObject;
    rootObject = [NSMutableDictionary dictionary];
    for(Node* node in [sharedValues nodes]){
        NSLog(@"Node has color %@ and tag %ld.", [node originalColor], [node tag]);
    }
    [rootObject setValue: [imageView aPath] forKey:@"path"];
    [rootObject setValue:[sharedValues nodes] forKey:@"nodes"];
    [NSKeyedArchiver archiveRootObject: rootObject toFile: [path path]];
}

The load method:

- (void)loadDataFromDiskWithPath:(NSURL*)path
{
    NSDictionary * rootObject;

    rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[path path]];    
    [imageView setAPath:[rootObject valueForKey:@"path"]];
    [sharedValues setNodes:[rootObject valueForKey:@"nodes"]];
    [imageView setNeedsDisplay:YES];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-11-14 08:14:25

您必须采用 NSCoding 协议并实现 -initWithCoder:-encodeWithCoder: 方法才能正确归档对象。由于Node是采用该协议的UIView的子类,因此它的某些部分正在正确归档。这些是 UIView 及其父类的属性。为了确保正确归档,Node 也必须实现该协议。浏览 此文档以获得更多见解。

You will have to adopt the NSCoding protocol and implement -initWithCoder: and -encodeWithCoder: methods for the object to archive properly. Since Node is a subclass of UIView which adopts the protocol, some parts of it are being archived correctly. These are the properties of UIView and its parent classes. To ensure proper archiving, Node will have to implement the protocol too. Go through this doc for additional insight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文