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

For ease of use, I am writing an SBJsonParser category with the name Add-on:

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

My questions:

  • Is it correct?
  • Is the pointer parser in the class method static? If not, should I declare it static?
  • should a pointer parser be released?
+4
source share
4 answers
  • It looks good if you know for sure that the JSON data contains a dictionary, not an array.

  • No and no. parser is local and does not need anything else if the -objectWithData: method is synchronous.

  • Yes.

+2
source

It looks well formed and overall correct.

You do not need to use a static keyword for the parser in this case.

Yes, you need to free the parser, and you did it in the right place.

+1
source

1) looks good

2) it is not. You should not.

3) this is normal since it

+1
source

It's good. Parser is not a status, it is a regular local variable, but since you generally release it, then what. Yes, the parser must be released

+1
source

All Articles