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:
- 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?
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.
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
精彩评论