문제

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