I’m not sure what I’m doing wrong here. I’ve also tried setting s1..3 in foo by using:
s1 = [[NSString alloc] initWithString:[filepaths objectAtIndex:0]];
Context below:
void foo(NSString *s1, NSString *s2, NSString *s3){
//assign long string to NSString *fps
//...
//break fps into smaller bits
NSArray *filepaths = [fps componentsSeparatedByString:@"\n"];
//the above worked! now let's assign them to the pointers
s1 = [filepaths objectAtIndex:0];
//repeat for s2 and s3
NSLog(@"%@",s1); //it worked! we're done in this function
}
int main(int argc, const char * argv[]){
NSString *s1 = nil; //s2 and s3 as well
foo(s1,s2,s3); //this should work
NSLog(@"%@",s1); //UH OH, this is null!
return 0;
}
No.
You are passing in pointers to objects which can be mutated locally. You are not changing the original objects, as you might think from plain C.
If you want to use this method (which I would not recommend – it’s really odd to see in Cocoa except in the case of
NSError), you would have something like:You would then pass in
&s1as the argument.This will, of course, clobber whatever was in
s1, potentially cause memory leaks, thread unsafety, etc., unless you are really careful. Which is why I say you usually won’t do this.