我是iphone的初学者,在我的代码中由于未捕获的异常'NSInvalidArgumentException'而导致终止应用程序的运行时错误,原因:' - [__ NSCFString _isResizable]:无法识别的选择器发送到实例0x6e6e300'
我的代码是
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path=[[NSBundle mainBundle] pathForResource:@"Animalfile" ofType:@"plist"];
NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:path];
NSArray *animal=[dict valueForKey:@"image"];
NSLog(@"print:%@",dict);
NSLog(@"hello:%@",animal);
UIImage *img=[animal objectAtIndex:currentImage];
[animalphoto setImage:img];
}
Run Code Online (Sandbox Code Playgroud)
提供适用于我的代码的任何建议和源代码....
das*_*ght 11
当您尝试将字符串视为图像时会发生此问题:
UIImage *img = [animal objectAtIndex:currentImage];
Run Code Online (Sandbox Code Playgroud)
animal数组中的值不能是图像,因为该数组来自dict您使用该dictionaryWithContentsOfFile:方法从文件中读取的字典.这个方法将创建的对象类型NSString,NSData,NSDate,NSNumber,NSArray,或NSDictionary.UIImage不在该dictionaryWithContentsOfFile:方法可以创建的类型列表中,因此您的强制转换无效.
从错误消息中可以看出您正在获取NSString而不是UIImage.检查该字符串的值以查看它所代表的内容:它可能是URL,文件名或可用于获取图像的其他形式的标识符.根据字符串中的内容,将程序更改为加载,img而不是根据字符串的值.也许代码应该是
UIImage *img = [UIImage imageNamed:[animal objectAtIndex:currentImage]];
Run Code Online (Sandbox Code Playgroud)
但是如果不知道字符串的价值,就无法确定.
| 归档时间: |
|
| 查看次数: |
4229 次 |
| 最近记录: |