I need to use a method that returns several strings, different ones, according to a value.
My code looks like:
- (void) returnStringsAccordingToSet:(NSString *)string1: (NSString *)string2: (NSInteger)setNo { switch (setNo){ case 1: if (generalStringSettings){ string1 = @"The first string"; string2 = @"The second string"; } else { string1 = @"The first other string"; string2 = @"The second other string"; } break; case 2: ... break; case 3: ... break; } }I call that method with:
NSString *firstString = [[NSString alloc]init]; NSString *secondString = [[NSString alloc]init]; NSUInteger set = 1; [self getStringsAccordingToSet: firstString: secondString: set];
I can’t get it to work! All I get is empty strings. I’ve got a feeling that the call for the strings is somewhat wrong. Please help.
You could do this by filling a NSArray, but just to show how C works I’ll show the error that you made.
string1 and string2 are just pointers, so you can pass either mutable or immutable strings as arguments.But the original pointer never gets modified, for example:
Then when you call a method a copy is made for every pointer that you pass to the method:
The solutions: