5 に答える
Until you don't initialize your pointer (color4), it points to an undefined location, at which there's possibly no object, just garbage. Maybe it's not even a valid menory location. When dereferencing that, it will crash narurally. You need to initialize it to a valid object.
Furthermore, that object cannot be nil
or else NSMutableArray itself will throw an exception.
color4 is not initialized and you try to insert nil in your array. Initialize color4 like the other colors.
Color4 is nil when you add it to the array. NSMutableArrays are nil terminated. This means that the last element in an NSMutableArray is nil. You can't manually add nil objects to an array, this means that data after the nil object would be ignored!
The following code produces an exception:
NSMutableArray *array = [[NSMutableArray alloc] init];
UIColor *color;
[array addObject:color];
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
The solution is to initialise the color4 variable before adding it to the array.
試す:
UIColor* color1 = [UIColor blueColor];
UIColor* color2 = [UIColor greenColor];
UIColor* color3 = [UIColor whiteColor];
UIColor* color4 = [[UIColor alloc] init];
NSMutableArray* arrayColor = [[NSMutableArray alloc] initWithObjects:color1, color2, nil ];
[arrayColor addObject:color3];
[arrayColor addObject:color4];
you need to create first UIColor object and add in array like this
NSArray *mycolorsArray = [[NSArray alloc] initWithObjects:color1, color2, nil];