krackmoe
krackmoe

Reputation: 1763

NSMutuableString modification

I am new to objective-c and I do not understand completely, why the following is working. Why don't I have to return the string from the private method so that in the validateAcessCode method the string will be changed? Is it because the NSMutuableString works in local methods with the same reference of the String that I passed to it? Is this the reason?

- (void)replaceCharachters:(NSMutableString *)code {
    [code replaceOccurrencesOfString: @"J" withString: @"a" options:0 range:NSMakeRange(0, [code length])];
    [code replaceOccurrencesOfString: @"H" withString: @"b" options:0 range:NSMakeRange(0, [code length])];
    [code replaceOccurrencesOfString: @"Y" withString: @"c" options:0 range:NSMakeRange(0, [code length])];
}

-(IBAction)validateAccessCode:(id)sender {

    NSMutableString *code = [NSMutableString stringWithFormat:@"%@", accessCode.text];
    [self replaceCharachters:code];
}

Upvotes: 1

Views: 66

Answers (2)

Dennis Traub
Dennis Traub

Reputation: 51634

You're just working with a pointer to the actual string. Both methods use that pointer so they access the same object in memory.

Upvotes: 1

mah
mah

Reputation: 39807

The validateAccessCode: method is being called by sender which presumably is a UI object that has a text field. This method is changing the text in the field when it calls replaceCharachters:, so there's no need to return anything.

Upvotes: 0

Related Questions