Andreas Prang
Andreas Prang

Reputation: 2217

Different behavior of __weak on NSURL and NSString

Whats the difference between NSString and NSURL on __weak ?

My example:

__weak NSURL *myURL = [NSURL fileURLWithPath:@"/tmp"];
__weak NSString *myString = @"123";

NSLog(@"myURL: %@", myURL);
NSLog(@"myString: %@", myString);

The result:

2012-07-10 19:23:49.858 myApp[56093:303] myURL: (null)

2012-07-10 19:23:49.859 myApp[56093:303] myString: 123

Why isn't the result myString == (null)

Upvotes: 1

Views: 93

Answers (2)

Slim Shaddy
Slim Shaddy

Reputation: 138

Try this

NSLog(@"myURL: %@", [myURL description]);

NSLog(@"myString: %@", myString);

As "%@" is used to represent NSString not NSURL.

Upvotes: -1

David Rönnqvist
David Rönnqvist

Reputation: 56625

This is because the string in your example is a string literal that never gets released. Your property doesn't retain the string but since it doesn't get released it still points to the value you assigned it.

If you would have created the string using [NSString stringWithFormat:@"hello"]; then you would see the expected behavior.

Upvotes: 6

Related Questions