我可以写一个像这样的 Objective-C 类方法吗?
为了方便使用,我写了一个SBJsonParser类别,名为Addition:
@implementation SBJsonParser(Addition)
+ (NSDictionary *)parseJson:(NSData *)data {
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithData:data];
[parser release];
return dict;
}
@end
我的问题是:
- 是吗?
- 类方法中的指针解析器是静态的?如果不是,我应该将其声明为静态吗?
- 指针解析器需要释放吗?
For convenience to use,I write a SBJsonParser Category named Addition:
@implementation SBJsonParser(Addition)
+ (NSDictionary *)parseJson:(NSData *)data {
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithData:data];
[parser release];
return dict;
}
@end
My questions are:
- Is it right?
- The pointer parser in class method is static?If not,should I declare it to static?
- the pointer parser needs to release?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您确定 JSON 数据包含字典而不是数组,则看起来不错。
不,不。
parser
是本地的,如果-objectWithData:
方法是同步的,则不需要是其他任何东西。是的。
Looks okay, if you know for sure that the JSON data contains a dictionary and not an array.
No, and no.
parser
is local, and doesn't need to be anything else if the-objectWithData:
method is synchronous.Yes.
这看起来格式良好且总体正确。
在这种情况下,您不必使用解析器的 static 关键字。
是的,您需要释放解析器并且您已经在正确的位置完成了它。
This looks to be well formed and correct overall
You don't have to use the static keyword for parser in the case.
Yes, you need to release parser and you've done it in the correct place.
1)看起来不错
2)事实并非如此。你不应该。
3) 就这样就好了
1) it looks fine
2) it is not. you should not.
3) it is fine as it is
很好。解析器不是状态,它是常规局部变量,但既然你无论如何都释放它,那又怎样。是的,需要释放解析器
It fine. Parser is not status it is regular local variable but since you release it anyway so what. Yes parser needs to be released