NSKeyedArchiver .plist 附加问题

发布于 2024-11-14 08:08:26 字数 1243 浏览 6 评论 0 原文

**

  1. 我正在将数据从表单获取到数组中,并尝试使用 NSKeyedArchiver 将该数组写入 .plist 文件。
  2. 写入成功,但是每当我运行脚本时它都会覆盖以前的数据。
  3. 如何将新数据附加到 .plist 文件而不是覆盖?

**

@implementation Player

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (IBAction)savePlayer:(id)sender {
    NSString *path = @"/Users/username/fm.plist";
    NSString *pl= [teamPlayer stringValue];
    NSString *name = [namePlayer stringValue];
    NSString *age = [agePlayer stringValue];
    NSString *position= [positionPlayer stringValue];
    Player *player = [[Player alloc] init];

    array = [[NSMutableArray alloc] initWithObjects:pl,
                                  name, age, position, nil];

    [NSKeyedArchiver archiveRootObject:array toFile:path];
    NSString *ns = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"test: %@" , ns);
    [array release];
}

- (void) encodeWithCoder: (NSCoder *) coder{
    [coder encodeObject:array forKey:@"someArray"];
}

- (void) decodeWithCoder: (NSCoder *) coder{
    [coder decodeObjectForKey:@"someArray"];
    return self;
}

**

  1. I am getting data from a form into an array and trying to write that array into a .plist file using NSKeyedArchiver.
  2. Writing is successful,however it overwrites to the previous data whenever I run the script.
  3. How can I append the new data to .plist file instead of overwriting?

**

@implementation Player

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (IBAction)savePlayer:(id)sender {
    NSString *path = @"/Users/username/fm.plist";
    NSString *pl= [teamPlayer stringValue];
    NSString *name = [namePlayer stringValue];
    NSString *age = [agePlayer stringValue];
    NSString *position= [positionPlayer stringValue];
    Player *player = [[Player alloc] init];

    array = [[NSMutableArray alloc] initWithObjects:pl,
                                  name, age, position, nil];

    [NSKeyedArchiver archiveRootObject:array toFile:path];
    NSString *ns = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"test: %@" , ns);
    [array release];
}

- (void) encodeWithCoder: (NSCoder *) coder{
    [coder encodeObject:array forKey:@"someArray"];
}

- (void) decodeWithCoder: (NSCoder *) coder{
    [coder decodeObjectForKey:@"someArray"];
    return self;
}

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

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

发布评论

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

评论(1

没有伤那来痛 2024-11-21 08:08:26

首先,最后两个方法似乎不相关,因为您没有对这些方法所属的对象进行编码。同样,NSCoding 协议包含 encodeWithCoder:initWithCoder: 方法。 NSCoding 协议中没有 decodeWithCoder: 方法。

其次,您将创建一个新的 NSMutableArray 对象,该对象用几个元素进行初始化,并将其归档到一个文件中,以便它覆盖现有的对象。您需要通过取消归档文件来获取现有数组,创建可变副本,然后附加值。所以代码会是这样的,

NSArray *existingValues = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSArray *newValues = [existingValues copy];
[newValues addObjectsFromArray:[NSArray arrayWithObjects:pl, name, age, position, nil]];
[NSKeyedArchiver archiveRootObject:newValues toFile:path];
[newValues release];

First of all, the last two methods don't seem to be relevant as you aren't encoding whichever object those methods belong to. Again, the NSCoding protocol includes encodeWithCoder: and initWithCoder: methods. There is no decodeWithCoder: method in the NSCoding protocol.

Secondly, you are creating a new NSMutableArray object initialized with few elements and archiving it to a file so it writes over the existing one. You will need to get the existing array through unarchiving the file, create a mutable copy and then appending the values. So code will be something like this,

NSArray *existingValues = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSArray *newValues = [existingValues copy];
[newValues addObjectsFromArray:[NSArray arrayWithObjects:pl, name, age, position, nil]];
[NSKeyedArchiver archiveRootObject:newValues toFile:path];
[newValues release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文