Play with ARC: Annoyance in the power of liberation?

I am currently chatting a bit with ARC to figure out how to do this before starting to do my job. I installed this code:

NSNumber* n = [[NSNumber alloc] initWithInt:3];
__weak NSNumber* weakN = n;
n = nil;
NSLog(@">>>: %@ %@", n, weakN);

I expected n and weak N to be nil, since n = nil; should cause a surge in my eyes? Unfortunately, this does not happen. The output is "→>: (null) 3". What am I missing here?

Another thing is that I'm sure the code below gave me a hard time starting from the arc:

__weak NSNumber* weakN2 = [[NSNumber alloc] initWithInt:3];
NSLog(@">>>: %@", weakN2);

I am sure I had some problems with similar code, since arc would release the object immediately after initialization, since there is no strong reference to the object. Unfortunately, the exit from the above "→>: 3".

It would be great to get some clarification on this. I am clearly missing something!

Regards, Michael

+3
3

, kevboh, Foundation, NSNumber. Foundation . , .

, , , [[NSNumber alloc] initWithInt:3], , .

+3

, , . NSString ( ), :

NSString* n = [[NSString alloc] initWithFormat:@"3"];
__weak NSString* weakN = n;
n = nil;
NSLog(@">>>: %@ %@", n, weakN);
// Output is (null) (null)

__weak NSString* weakN2 = [[NSString alloc] initWithFormat:@"3"];
NSLog(@">>>: %@", weakN2);
// Output is (null)

NSNumber , , , . , , . ( NSString* n = @"3";)

+3

, n N nil, n = nil; ? , . " → > : (null) 3". ?

ARC . ; ARC, , . , deallocs , / , .

+1

All Articles