I have corrected something that was confusing me in V2 but am still a little curious as to what V1 is doing. Looking at V1 now am I right in thinking that line is being passed by value (so essentially what I have is a local copy of line). Scanner then scans its data into the address of that local copy, the local copy is destroyed when the method exits and the external variable line is still nil?
On V2, I am passing the address of line and then scanning data into that address, is this using passing by reference, just not sure of the terminology?
// V1
NSString *line = nil;
[self scanUsing:scanner into:line];
NSLog(@"AFTER_: %@", line);
- (void)scanUsing:(NSScanner *)scanner into:(NSString *)line {
[scanner scanUpToString:@"\n" intoString:&line];
NSLog(@"INSIDE: %@", line);
}
.
// V2
NSString *line = nil;
[self scanUsing:scanner into:&line];
NSLog(@"AFTER_: %@", line);
- (void)scanUsing:(NSScanner *)scanner into:(NSString **)line {
[scanner scanUpToString:@"\n" intoString:line];
NSLog(@"INSIDE: %@", *line);
}
V1
You are passing a copy of the pointer. It points to the same memory region, so what you see is the same value. You are passing the object then, by value. You can change the content, but not create a new object, since that pointer wont exist when the method finishes.
V2
Definition of reference is different (its a C++ type) But yeah, lets say that it behaves more or less the same. In V2 a new object can be allocated inside the method, and therefore, you can change the memory region that is being pointed by it.
So: