質問

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