开发者

Can I write a Objective-C class method like this?

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:da开发者_运维知识库ta]; 
    [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?


  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.


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) 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜