开发者

Confused about enumeration... How do I change the objects that during enumeration?

Still new to Objective-C and I'm confused about enumeration...

Let's say I have an NSArray of 8 "Car" objects and all of them have their "pain开发者_运维百科tColor" property set to "red. I want to enumerate through the array and set each Car.paintColor property to "blue". How do I do that?

for (Car *car in myArray){
    car.paintColor = @"blue";  
}

But then if I do

Car *testCar = [myArray objectAtIndex:2];

NSLog(@"The car color for 3rd object in myArray is %@", testCar.color);

I still get "red". So it seems that enumeration just creates a temporary object but doesn't modify the actual object that is contained in the array.

What is the proper way to do what I'm trying to do? I have lots of situations where I want to enumerate through an array but actually changes the items that are inside the array

Thanks

--EDIT

Ok, thanks for that information.. can you explain why this doesn't work?

I have an NSArray called "carsArray" set up as a property that contains three NSMutable string objects ("Buick", "Bronco", "Cadillac").

I then do

for (NSMutableString *car in myArray) {
    car = [NSMutableString stringWithFormat:@"test"];
}

NSMutableString *temp = [myArray objectAtIndex:2];
NSLog(@"third car is %@", temp);

NSLog is saying "Cadillac". Shouldn't it be "test"?


To do what you want:

for (NSMutableString *car in myArray) {
    [car setString:[NSMutableString stringWithFormat:@"test"]];
}

Basic understanding of pointers. It's not particularly enumeration that makes it work this way.

NSMutableString *car1 = [NSMutableString stringWithFormat:@"Cadillac"];
NSMutableString *car = car1;
[car setString:[NSMutableString stringWithFormat:@"Ford"]];
NSLog(@"car1 = %@", car1);

// Ford

car = [NSMutableString stringWithFormat:@"Buick"];
NSLog(@"car1 = %@", car1);

// Ford

When you set car = car1, car is an "alias" for car1. You can set properties on car, and the properties will be set on car1. You can call methods like setString on car, the method will be called on car1. car and car1 are pointers, they point to the same object.

When you assign a new value to car, it just makes it an alias for a different object and no longer the same object as car1. The assignment won't affect car1, nor will any property accessors or methods you do on car after that.

The enumeration effectively does car = myArray[0], car = myArray[1], car = myArray[2] in turn. (Shorthand: myArray[0] is not correct Objective-C syntax.)


Fast enumeration does not copy objects nor make temporary copies. The NSArray is an array of pointers to objects. The fast enumeration simply loops thru that list of pointers and each time through the loop you get the next pointer. The pointer points to the same object that the array points to.

The way you are changing the content of an object in the fast enumeration loop is fine.

Your problem is elsewhere (see Mattia's comment).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜