Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ramy1989

macrumors newbie
Original poster
Nov 7, 2012
21
0
I started learning Cocoa when there still was the manual counting, but after few time I got the new OS update and ARC was available.
I know how to user retain-release if I don't use ARC, but the problem comes with ARC enabled.I always used strong references and everything was fine, never had leaks nor dangling pointers.
But I want to understand better how it works.

Now I am running a project for debugging, with zombies enabled.I tried do do this:
Code:
    __unsafe_unretained NSString* str;
    @autoreleasepool
    {
        str= @"Hello";
    }
    NSLog(@"%@",str);

I would except that str is zombie because @"Hello" should be autoreleased, and I print it outside the pool.But instead it just prints "Hello" and all goes fine.Why?

Also a tip, should I start making some projects with manual counting to understand how ARC works?
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
I started learning Cocoa when there still was the manual counting, but after few time I got the new OS update and ARC was available.
I know how to user retain-release if I don't use ARC, but the problem comes with ARC enabled.I always used strong references and everything was fine, never had leaks nor dangling pointers.
But I want to understand better how it works.

Now I am running a project for debugging, with zombies enabled.I tried do do this:
Code:
    __unsafe_unretained NSString* str;
    @autoreleasepool
    {
        str= @"Hello";
    }
    NSLog(@"%@",str);

I would except that str is zombie because @"Hello" should be autoreleased, and I print it outside the pool.But instead it just prints "Hello" and all goes fine.Why?

Also a tip, should I start making some projects with manual counting to understand how ARC works?

Strings like @"Hello" are _never_ released and have never been released. Before ARC, their retain count returned some meaningless value, and retain / release / autorelease never had any effect on them.
 

Madd the Sane

macrumors 6502a
Nov 8, 2010
534
73
Utah
To get what you're looking for, use
Code:
str = [NSString stringWithString:@"Hello"]
.

I think NSString has a stringWithString method, but I can't verify it right now.
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
To get what you're looking for, use
Code:
str = [NSString stringWithString:@"Hello"]
.

I think NSString has a stringWithString method, but I can't verify it right now.

+stringWithString: will short-circuit to just using the argument directly in this case. -mutableCopy should get what he wants.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.