json-framework:stringWithObject 上的 EXC_BAD_ACCESS

发布于 2024-08-17 00:42:09 字数 1251 浏览 12 评论 0原文


更新:

我发现上一个错误的原因是文档中的错误。

该方法应该命名为 proxyForJson,而不是 jsonProxyObject...

但我仍然卡住了。

我现在在 stringWithObject 的某个地方收到 EXC_BAD_ACCESS 错误。有什么线索吗?


更新 2:

我的 proxyForJson 实现是从当时的文档中剪切粘贴的:

- (id)proxyForJson {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            Navn, @"Navn",
            Adresse, @"Adresse",
            Alder, @"Alder",
            nil];
}

尝试使 json 序列化适用于我的自定义 Objective-C 类。

据我了解文档, json-framework 可以序列化自定义对象,如果它们实现 jsonProxyObject 方法。

所以我有这个类:

@interface MyObject : NSObject {
    NSString *Name;
    NSString *Addresse;
    NSInteger Age;
}
@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Addresse;
@property (nonatomic, assign) NSInteger Age;
- (id)jsonProxyObject;
@end

我尝试序列化一个数组,其中包含一些实例:

[json stringWithObject:list error:&error];

但我得到的只是以下错误:

“MyObject 不支持 JSON 序列化”

我猜 jsonWriter 由于某种原因找不到我的 jsonProxyObject 方法,为什么买?

问候。


UPDATE:

I found that the reason for the previous error was an error in the documentation.

The method should be named proxyForJson, not jsonProxyObject...

But I'm still stuck, though.

I now get an EXC_BAD_ACCESS error inside stringWithObject some where. Any clues?


UPDATE 2:

My proxyForJson implementation is a cut-n-paste from then documentation:

- (id)proxyForJson {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            Navn, @"Navn",
            Adresse, @"Adresse",
            Alder, @"Alder",
            nil];
}

Trying to make json serialization work for my custom objective-c class.

As I understand the documentation, json-framework can serialize custom objects, if they implement the jsonProxyObject method.

So I have this class:

@interface MyObject : NSObject {
    NSString *Name;
    NSString *Addresse;
    NSInteger Age;
}
@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Addresse;
@property (nonatomic, assign) NSInteger Age;
- (id)jsonProxyObject;
@end

And I try to serialize an array with some instances in it:

[json stringWithObject:list error:&error];

But all I get is he following error:

"JSON serialisation not supported for MyObject"

I guess the jsonWriter can't find my jsonProxyObject method for some reason, buy why?

Regards.

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

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

发布评论

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

评论(3

暗喜 2024-08-24 00:42:09

您是否尝试在可执行信息窗格中打开 NSZombies 和 MallocStackLogging 来检查 EXC_BAD_ACCESS 的来源?如果没有,您可以尝试此操作并检查控制台的输出。
EXC_BAD_ACCESS 通常是由于某处过度释放对象而导致的错误。

Have you tried to turn on NSZombies and MallocStackLogging in your Executable Info pane to check the source of the EXC_BAD_ACCESS? If not, you might try this and check the console for the output.
EXC_BAD_ACCESS is often an error caused by over-releasing an object somewhere.

神妖 2024-08-24 00:42:09

我不确定这是否正确,但按如下方式定义类可以解决问题:

@interface MyObject : NSObject {
    NSString *Name;
    NSString *Addresse;
    NSInteger *Age;
}

@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Addresse;
@property (nonatomic, retain) NSInteger *Age;

- (id)jsonProxyObject;

@end

然后将变量初始化为:

Age = [[NSNumber alloc] initWithInt:32];

I am not sure whether this is the right thing to do but defining the class as follows, solves the problem:

@interface MyObject : NSObject {
    NSString *Name;
    NSString *Addresse;
    NSInteger *Age;
}

@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Addresse;
@property (nonatomic, retain) NSInteger *Age;

- (id)jsonProxyObject;

@end

Then initializing the variable as:

Age = [[NSNumber alloc] initWithInt:32];
愿与i 2024-08-24 00:42:09

在 NSString 中,没有 stringWithObject: 方法。您应该尝试使用 stringWithFormat: 代替。

In NSString, there is no stringWithObject: method. You should try using stringWithFormat: instead.

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