Using Link Passing in Objective-C

Looking at the source of the JSON-Framework, it makes heavy use of skip by reference in many parser method signatures. i.e.

@interface SBJsonParser ()
  - (BOOL)scanValue:(NSObject **)o;
  - (BOOL)scanRestOfArray:(NSMutableArray **)o;
  - (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o;
@end

The result is something like this:

id o;
[self scanValue:&o];
// Do something with o

- (BOOL)scanValue:(NSObject **)o {
  // Cut down for brevity
  return [self scanRestOfDictionary:(NSMutableDictionary **)o];
}

- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o {
  // Cut down for brevity
  *o = [NSMutableDictionary dictionaryWithCapacity:7];
  [*o setObject:@"value" forKey:@"key"];
  return YES;
}

What are the benefits of this approach?


EDIT: I ask more in terms of design. I understand what follows the link, I'm just wondering when it makes sense to use it. The design used in SBJsonParseris similar to the API used in NSScanner:

- (BOOL)scanUpToString:(NSString *)stopString intoString:(NSString **)stringValue;

For me, this means that the line that was scanned is secondary to the need to know if something was scanned. This is different from the API used NSString:

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

API , NSError , - .

, API , .

+3
2

"" . "o". , , .

BOOL ; .

+2

. API.

, , , .

? . .

? . .

, , , .

.

, : . .

? .

? , , .

+2

Source: https://habr.com/ru/post/1732454/


All Articles