NSString * string = @ "someString" vs NSString * string = [[NSString alloc] initWithFormat @ "% @", string]

If i have a method

- (void) myMethod:(NSString *)string {
    [Object anothermethodWithString:string];
}

and i call

[Object myMethod:@"this is a string with no alloc statement"]

I need to do something like

- (void) myMethod:(NSString *)string {
    NSString *string2 = [[NSString alloc] initWithFormat:@"%@", string];
    [Object anothermethodWithString:string2];
    [string2 release];
}

instead of having myMethod before? I have the wrong code, which is apparently caused by the fact that the line is automatically freed, and the second method in another method is called (as in the example). Secondly, I have myMethod that fixed all my problems.

So does the line "non-alloc" contain a line with auto-release? I asked this question as an answer to another question (which was completely unrelated and why I am creating this post), and several sources said that I did not need to redistribute the line. I am confused because the behavior of my code tells me otherwise.

+3
2

. release , new, alloc, retain copy.

, , Apple. .

+4

: NSString * str = @"". , .

: NSString * str = [NSString stringWithFormat: @""], str .

, init. .

: - , , , , , ,

+2

Source: https://habr.com/ru/post/1755081/


All Articles