Вопрос

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];
}
Это было полезно?

Решение

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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top