Although I usually ARC mode in Xcode, but sometimes, I still need to release some temp obj. I don’t know how to use it. Here is a example:
TestViewController.h file
TestViewController
{
A* a;
A* b;
}
TestViewController.m file
viewDidLoad()
{
a = [[A alloc] init];
b = a;
[a release];
// I want to release a here, but when I use [a release], there will be a build error. "release is unavailable: not available in ARC mode"
}
Can anyone give me some clues?
ARC will take care of this for you. There is no reason to manually release
a. Doing so would be incorrect anyway, sinceastill points to the object.But you should still not directly access your ivars this way. While ARC will do the right thing (more on that in a second), it causes many other problems. Always use accessors except in
initanddealloc. This should be:As for what ARC is actually doing in your code, it will insert the required retains something like this:
If you don’t want
ato point to the old value, then set it tonilas Michael notes. This has nothing to do with retains and releases. Focus on the object graph. What do you want each thing to point to? ARC will take care of the retain counts.