目标 C:序列化/归档问题

发布于 2024-12-06 18:30:36 字数 1702 浏览 1 评论 0原文

我是一名 Android 开发者,最近开始开发 IOS。在我的所有 Android 项目中,我都有一个 IO 类,它执行以下操作:

public static Object load(String fname , Context cont)
{
    FileInputStream fis = cont.openFileInput(fname);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object loadedObject = ois.readObject();
    ois.close();
    fis.close();
    Father.print("Loaded from file "+fname+" successfully");
    return loadedObject;
}   

以及与保存等效的操作。它工作得很好,我可以用它从任何东西加载和保存实例。

在 Objective C 中,我使用 NSKeyedArchiver/NSKeyedUnarchiver。归档似乎有效,但取消归档会导致这种情况:

*** -[NSKeyedUnarchiver initForReadingWithData:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?

我正在使用以下静态方法:

+(BOOL)saveObject: (id<NSCoding>)obj forKey:(NSString*)key;
{
        NSKeyedArchiver *archiver;
        NSMutableData *bytes = [NSMutableData data];
        archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:bytes];
        [archiver encodeObject:obj forKey:key];
        [archiver finishEncoding];
        NSLog(@"Successfully saved object <%@> to %@", obj, key);
        return TRUE;
}

并且

+(id)loadForKey:(NSString*)key
{
    NSMutableData *bytes = [NSMutableData data];
    NSKeyedUnarchiver *unrar = [[NSKeyedUnarchiver alloc] initForReadingWithData:bytes];
    id result = [unrar decodeObjectForKey:key];
    [unrar finishDecoding];
    NSLog(@"Successfully loaded object <%@>", result);

    return result;
}

我这样称呼它们

[IVIO saveObject:thing forKey:@"thekey"];
Thing *miracle = [IVIO loadForKey:@"thekey"];

发生了什么事?

谢谢

I'm an Android dev who's recently started IOSing. In all my of my Android projects, I have an IO class that does the following:

public static Object load(String fname , Context cont)
{
    FileInputStream fis = cont.openFileInput(fname);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object loadedObject = ois.readObject();
    ois.close();
    fis.close();
    Father.print("Loaded from file "+fname+" successfully");
    return loadedObject;
}   

and the equivalent for save. It works great, I can load and save instances from anything with it.

In Objective C, I'm using NSKeyedArchiver/NSKeyedUnarchiver. Archiving seems to work, but unarchiving causes this:

*** -[NSKeyedUnarchiver initForReadingWithData:]: data is empty; did you forget to send -finishEncoding to the NSKeyedArchiver?

I'm using the following static methods:

+(BOOL)saveObject: (id<NSCoding>)obj forKey:(NSString*)key;
{
        NSKeyedArchiver *archiver;
        NSMutableData *bytes = [NSMutableData data];
        archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:bytes];
        [archiver encodeObject:obj forKey:key];
        [archiver finishEncoding];
        NSLog(@"Successfully saved object <%@> to %@", obj, key);
        return TRUE;
}

and

+(id)loadForKey:(NSString*)key
{
    NSMutableData *bytes = [NSMutableData data];
    NSKeyedUnarchiver *unrar = [[NSKeyedUnarchiver alloc] initForReadingWithData:bytes];
    id result = [unrar decodeObjectForKey:key];
    [unrar finishDecoding];
    NSLog(@"Successfully loaded object <%@>", result);

    return result;
}

I'm calling them as such

[IVIO saveObject:thing forKey:@"thekey"];
Thing *miracle = [IVIO loadForKey:@"thekey"];

What's going on?

Thank you

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

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

发布评论

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

评论(1

彩扇题诗 2024-12-13 18:30:36

在您的 +loadForKey: 中,

NSMutableData *bytes = [NSMutableData data];
NSKeyedUnarchiver *unrar = [[NSKeyedUnarchiver alloc] initForReadingWithData:bytes];

您尝试从空数据对象中读取。使用 [NSMutableData data] 创建可变数据对象只会为您提供一个不包含任何内容的对象。您需要从某个地方读取数据。同样,在 +saveObject:forKey: 中,您成功归档了对象,但没有对生成的数据对象执行任何操作。

要解决此问题,您需要在保存方法中将数据写入文件或 URL,并在加载方法中从文件或 URL 中读取数据。

In your +loadForKey:

NSMutableData *bytes = [NSMutableData data];
NSKeyedUnarchiver *unrar = [[NSKeyedUnarchiver alloc] initForReadingWithData:bytes];

you're trying to read from an empty data object. Creating a mutable data object using [NSMutableData data] just gives you an object that contains nothing. You'll need to READ the data from somewhere. Likewise, in your +saveObject:forKey: you successfully archive the object, but you don't do anything with the resulting data object.

To fix, you'll want to write the data into a file or URL in your save method, and read it from a file or URL in your load method.

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