NSArrayM 异常 insertObject:atindex
exception in NSArrayM insertObject:atindex
我在项目中使用了 4 张图像..而 运行 结果是:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
我的代码:
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[self.view addSubview:slowAnimationImageView];
[slowAnimationImageView startAnimating];
}
您遇到问题是因为您在数组中提供的图像名称在资源中不可用。检查数组中的最后一个对象:@"jake_5.png "。里面多了一个space。请删除它。这就是导致此问题的原因。
更新:
对于动画,您需要在将所有图像添加到您的imageArray 后进行设置。请参考此代码以获取帮助并进行更改:
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];
希望对您有所帮助...
最后一张图片的名字Space没有错
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for image in imagesNames
{
[images addObject:[image];
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[self.view addSubview:slowAnimationImageView];
[slowAnimationImageView startAnimating];
}
但如果您不想像这样添加 for 循环,请使用方法 addObjectOfArray 将对象附加到可变数组
出现问题是因为@"jake_5.png "
中多了一个space:
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
应该是:
@"jake_5.png"
附录:
您希望 UIImageView
为一系列图像制作动画的方式不正确:
请将您的替换为以下内容:
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
[self.view addSubview:slowAnimationImageView];
NSArray *imageNames = @[@"jake_2.png", @"jake_3.png", @"jake_4.png", @"jake_5.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "]; replace it with
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
因为你把 space @"jake_5.png " 可能是这个图像在资源中不可用所以它给你错误。
我在项目中使用了 4 张图像..而 运行 结果是:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack:
我的代码:
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[self.view addSubview:slowAnimationImageView];
[slowAnimationImageView startAnimating];
}
您遇到问题是因为您在数组中提供的图像名称在资源中不可用。检查数组中的最后一个对象:@"jake_5.png "。里面多了一个space。请删除它。这就是导致此问题的原因。
更新:
对于动画,您需要在将所有图像添加到您的imageArray 后进行设置。请参考此代码以获取帮助并进行更改:
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];
希望对您有所帮助...
最后一张图片的名字Space没有错
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *images = [[NSMutableArray alloc] init];
for image in imagesNames
{
[images addObject:[image];
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[self.view addSubview:slowAnimationImageView];
[slowAnimationImageView startAnimating];
}
但如果您不想像这样添加 for 循环,请使用方法 addObjectOfArray 将对象附加到可变数组
出现问题是因为@"jake_5.png "
中多了一个space:
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "];
应该是:
@"jake_5.png"
附录:
您希望 UIImageView
为一系列图像制作动画的方式不正确:
请将您的替换为以下内容:
UIImageView *slowAnimationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(160, 95, 86, 193)];
[self.view addSubview:slowAnimationImageView];
NSArray *imageNames = @[@"jake_2.png", @"jake_3.png", @"jake_4.png", @"jake_5.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
slowAnimationImageView.animationImages = images;
slowAnimationImageView.animationDuration = 5;
[slowAnimationImageView startAnimating];
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png "]; replace it with
NSArray *imageNames= @[@"jake_2.png",@"jake_3.png",@"jake_4.png",@"jake_5.png"];
因为你把 space @"jake_5.png " 可能是这个图像在资源中不可用所以它给你错误。