25

I have the following code and am getting this error before compiling:

Fast Enumeration Variables can't be modified in ARC by default, declare the variable _strong to allow this

for (NSString *name in array){
        @try {
            S3ObjectController *localS3 = [[S3ObjectController alloc]init];
            name = localS3.stringProperty;
}

In this S3ObjectController class, I have the property declared like this:

@property (nonatomic, strong)  NSString *stringProperty;

How should I change the property? I thought I was declaring it strong?

jscs
  • 63,694
  • 13
  • 151
  • 195
Eric
  • 4,063
  • 2
  • 27
  • 49
  • Looks to me it should be `localS3.stringProperty = name;`, not the other way around. – Kyr Dunenkoff Jan 23 '12 at 15:15
  • 3
    Do you guys know WHY this error occurs? The default is __strong, so the variable `name` should have already been `__strong` in the for loop as it existed? `for (NSString *name in array)`. – Kurt Spindler Mar 16 '12 at 00:25
  • possible duplicate of [Why is \_\_strong required in fast enumeration loops with ARC](http://stackoverflow.com/questions/18387281/why-is-strong-required-in-fast-enumeration-loops-with-arc) – jtbandes Jul 10 '14 at 22:39
  • @KurtSpindler: I'm ten years late, but while the default in normal code is `__strong`, in fast enumeration variables, the default is (implicitly) `const __strong`. So, by explicitly declaring it `__strong`, you allow it to be modified from within the loop. – NSGod Nov 03 '22 at 16:36

1 Answers1

59

It means declare the fast enumeration variable strong, not your instance variable:

for (NSString __strong *name in array) {
    @try {
        S3ObjectController *localS3 = [[S3ObjectController alloc]init];
        name = localS3.stringProperty;
    }
}
Stuart
  • 36,683
  • 19
  • 101
  • 139