I’m developing an iPhone app.
I have the following INIT code:
shapes = [NSMutableArray arrayWithCapacity:numShapes];
After that, I want to do the following:
- (CGSize) sizeOfShapeType:(ShapeType)type{
CGSize shapeSize = CGSizeMake(0, 0);
if (shapes != nil) {
for(Object2D* obj in shapes)
if (obj.figure == type) {
shapeSize = obj.size;
break;
}
}
return (shapeSize);
}
But I always get a EXEC_BAD_ACCESS because all shapes in shapes array are null.
How can I check if an Object2D is null?
I get the exception here:
for(Object2D* obj in shapes)
arrayWithCapacity returns autoreleased object, so you must retain it to make sure it won’t get deallocated prematurely:
or
For the latter solution you need to declare property with retain attribute for shapes ivar.