1
4

5 に答える 5

6

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.

于 2012-08-03T05:52:30.873 に答える
3

color4 is not initialized and you try to insert nil in your array. Initialize color4 like the other colors.

于 2012-08-03T05:50:43.657 に答える
2

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.

于 2012-08-03T05:51:02.210 に答える
0

試す:

 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];
于 2012-08-03T05:56:22.270 に答える
0

you need to create first UIColor object and add in array like this

  NSArray *mycolorsArray = [[NSArray alloc] initWithObjects:color1, color2, nil];
于 2012-08-03T06:30:58.640 に答える