0

My class has some @properties (strong); apples, bananas and oranges of NSArray* type; and I'm wondering if this:

for(NSArray* __strong fruit in @[apples, bananas, oranges])     {
    fruit = [fruit sortedArrayUsingComparator:comparator];
}

is the same as this:

apples  = [apples  sortedArrayUsingComparator:comparator];
bananas = [bananas sortedArrayUsingComparator:comparator];
oranges = [oranges sortedArrayUsingComparator:comparator];

comparator is an NSComparator.

I think they should be the same but I'm not sure how the __strong relates to the for loop in this context.

Josh Paradroid
  • 1,172
  • 18
  • 45
  • 2
    It should be noted that the first example is a Fruit Loop. – Hot Licks Feb 07 '13 at 13:07
  • 2
    The only significance of declaring the loop variable as `__strong` is that it allows you to modify the loop variable. By default, loop variables declared in the condition of a fast enumeration loop are `__weak` and *immutable*, see e.g. http://stackoverflow.com/questions/14209076/setting-objects-to-nil-during-fast-enumeration/14209189#14209189. - But you modify only the local variable, not the original arrays, as already said in the answers. – Martin R Feb 07 '13 at 13:25

2 Answers2

2

this is not the same code, the loop will not change the values of the variables apples, bananas and oranges

in your loop you assigning the sort-result to the local variable fruit, this will not affect the contents of the values stored in apples, bananas or oranges.

in the 'unrolled' code you assigning the sort-result to the original variables, therefore overwriting the content of this variables.

Also i think your loop-type Fruit is wrong, unless apples, bananas and oranges are of type Fruit and not of type NSArray which the rest of the code suggests.

Jonathan Cichon
  • 4,396
  • 16
  • 19
1

No, it's not the same , the same would be:

NSArray* fruits= [apples sortedArrayUsingComparator: comparator ];
fruits= [bananas sortedArrayUsingComparator: comparator];
fruits= [oranges sortedArrayUsingComparator: comparator];

So the first two sorts are useless, because you assign fruits just to the last sorted array: oranges.

A possible solution

It's not clear what you want to achieve, maybe something like this:

NSArray* sortedFruits;
for(NSArray* fruit in @[apples, bananas, oranges])     
{
    NSArray* sorted = [fruit sortedArrayUsingComparator:comparator];
    [sortedFruits addObject: sorted];
}

This way you get an array with 3 items: the sort results of apples, bananas and oranges.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • I'm using ARC (I guess I should have mentioned that – I'll tag it) and if I remove the __strong, I get an error about modifying a fast enumeration variable. Is there a correct way to (and this is probably not the right terminology) 'roll up' my expanded code into a for loop? – Josh Paradroid Feb 07 '13 at 13:17
  • The problem is that you are using fruit to enumerate over the array, but also assigning it to the enumerated values. Martin R 's comment explains this very well. – Ramy Al Zuhouri Feb 07 '13 at 13:26
  • I understand this now. I'm just wondering if there's a better way to do what my expanded (and correct) code was doing, perhaps in some other kind of loop. – Josh Paradroid Feb 07 '13 at 13:36
  • 1
    I've written down a possible solution. – Ramy Al Zuhouri Feb 07 '13 at 13:42
  • 1
    @JoshParadroid: If you declare apples, bananas and oranges as *mutable arrays*, then you can sort them in the loop with `[fruit sortUsingComparator:comparator]`. – Martin R Feb 07 '13 at 13:46
  • @RamyAlZuhouri Thanks. That's almost exactly what I wanted to achieve, only I was hoping to keep the arrays as their original variable names, which the unrolled version does. – Josh Paradroid Feb 07 '13 at 15:19
  • @MartinR While this would work, in reality I'm sorting a number of iOS UIView and I'm not sure if there are any considerations regarding using mutable versus immutable element initialised via a nib. Thanks though. – Josh Paradroid Feb 07 '13 at 15:22