So I wrote this code:
NSString *[email protected]"string 1";
NSString *[email protected]"string 2";
NSLog(@"str2 is: %@", str2);
NSLog(@"str2 is: %@",str2);
and it gave the following output:
2015-03-01 17:11:16.600 er[956:32910] str2 is: string 2
2015-03-01 17:11:16.601 er[956:32910] str2 is: string 1string 2
so str2 now points to different location in memory. Is there a way to access the old pointer str1 or does arc kill it immediately?
Best How To :
Generally no, after you assign a new value to an object pointer the old object gets released. It will stay in memory of course if there are other pointers to it. So if you want to keep an object around keep a pointer to it.
NSString *oldStr2 = str2;
before reassigning str2 and you keep it. Otherwise you can not know if the object stays in memory or goes away. And if it stays in memory you don't know where it is as you don't have a pointer to it any more.
And even if you could somehow find this object without a reference to it still would be better code to make it explicit that you want to access it again later by setting a new reference.
There is an exception of course for constant strings as in your example. Those get built in to the executable and are always available - they can not go away. But you still can't access them any more if you don't have a pointer.