mardi 27 octobre 2009

NSArray features

We had an NSArray of objects of the type UIImageView.

the array was retained being a property etc

initialize like this:

testBall2Array = [NSMutableArray arrayWithObjects:
[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"testBall2.png"]],
[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"testBall2.png"]],
[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"testBall2.png"]],
[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"testBall2.png"]],
[[UIImageView alloc] initWithImage: [UIImage imageNamed:@"testBall2.png"]],
nil];

We succeeded in putting the views on screen in the same method but apparently in another method we got a bad access error when doing something with an element of this array.

The elements were not retained, but how to retain them?

This is one of the niceties of Xcode! :-(((

The answer is found here:
http://stackoverflow.com/questions/519467/iphone-getting-the-uiimageview-back-from-nsarray

you have to initialize with a different method:
initWithObjects

instead of

arrayWithObjects

the first retains the objects the second doesn't......

well this is the way you spent your time doing Xcode....