json-framework:stringWithObject 上的 EXC_BAD_ACCESS
更新:
我发现上一个错误的原因是文档中的错误。
该方法应该命名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试在可执行信息窗格中打开 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.
我不确定这是否正确,但按如下方式定义类可以解决问题:
然后将变量初始化为:
I am not sure whether this is the right thing to do but defining the class as follows, solves the problem:
Then initializing the variable as:
在 NSString 中,没有 stringWithObject: 方法。您应该尝试使用
stringWithFormat:
代替。In NSString, there is no
stringWithObject:
method. You should try usingstringWithFormat:
instead.