我可以写一个像这样的 Objective-C 类方法吗?

发布于 2024-12-03 10:41:19 字数 418 浏览 7 评论 0原文

为了方便使用,我写了一个SBJsonParser类别,名为Addition:

@implementation SBJsonParser(Addition)

+ (NSDictionary *)parseJson:(NSData *)data {
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *dict = [parser objectWithData:data]; 
    [parser release];
    return dict;
}

@end

我的问题是:

  1. 是吗?
  2. 类方法中的指针解析器是静态的?如果不是,我应该将其声明为静态吗?
  3. 指针解析器需要释放吗?

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:

  1. Is it right?
  2. The pointer parser in class method is static?If not,should I declare it to static?
  3. the pointer parser needs to release?

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

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

发布评论

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

评论(4

貪欢 2024-12-10 10:41:19
  1. 如果您确定 JSON 数据包含字典而不是数组,则看起来不错。

  2. 不,不。 parser 是本地的,如果 -objectWithData: 方法是同步的,则不需要是其他任何东西。

  3. 是的。

  1. Looks okay, if you know for sure that the JSON data contains a dictionary and not an array.

  2. No, and no. parser is local, and doesn't need to be anything else if the -objectWithData: method is synchronous.

  3. Yes.

羁〃客ぐ 2024-12-10 10:41:19

这看起来格式良好且总体正确。

在这种情况下,您不必使用解析器的 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.

你如我软肋 2024-12-10 10:41:19

1)看起来不错

2)事实并非如此。你不应该。

3) 就这样就好了

1) it looks fine

2) it is not. you should not.

3) it is fine as it is

冷弦 2024-12-10 10:41:19

很好。解析器不是状态,它是常规局部变量,但既然你无论如何都释放它,那又怎样。是的,需要释放解析器

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

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