I want to set an object to ‘nil’ as I enumerate through an array, as follows:
for(Object* object in array){
object = nil;
}
Xcode then tells me ‘Fast enumeration variables can’t be modified in ARC by default; declare the variable __strong to allow this.’
Which means doing this:
for(Object __strong* object in array){
object = nil;
}
This seems to be redundant. As far as I understand, declaring a strong reference to an object increases its retain count by one, and nil-ing it decreases the retain count by one. So how, then, do I set an object to nil while enumerating through an array?
I am using ARC.
See Fast enumeration iteration variables in the Clang “Objective-C Automatic Reference Counting” documentation:
So by default, the loop variable is immutable, and the retain count of the current object is not increased for performance reasons.
If you explicitly declare the loop variable as
__strong, it is a mutable strong reference, and the retain count of the current object is increased, and setting the loop variable tonildecreases the retain count again. But doing so does not deallocate the object or remove it from the array, because the array holds another strong reference to the object.