【问题标题】:how to solve run time error of [__NSCFString _isResizable]: unrecognized selector sent to instance?如何解决 [__NSCFString _isResizable] 的运行时错误:无法识别的选择器发送到实例?
【发布时间】:2012-07-07 10:29:10
【问题描述】:

我是 iphone 的初学者,在我的代码中由于未捕获的异常 'NSInvalidArgumentException' 而出现终止应用程序的运行时错误,原因:'-[__NSCFString _isResizable]: unrecognized selector sent to instance 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];

}

给出适用于我的代码的任何建议和源代码......

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    当您尝试将字符串视为图像时会出现问题:

    UIImage *img = [animal objectAtIndex:currentImage];
    

    animal 数组中的值不能是图像,因为该数组来自使用 dictionaryWithContentsOfFile: 方法从文件中读取的字典 dictThis method will create objects of types NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionaryUIImage 不在dictionaryWithContentsOfFile: 方法可以创建的类型列表中,因此您的转换无效。

    从错误消息看来,您收到的是NSString,而不是UIImage。检查该字符串的值以查看它所代表的内容:它可能是 URL、文件名或可用于获取图像的某种其他形式的标识符。根据字符串中的内容,将程序更改为加载img,而不是根据字符串的值。也许代码应该是

    UIImage *img = [UIImage imageNamed:[animal objectAtIndex:currentImage]];
    

    但如果不知道字符串的值,就无法确定。

    【讨论】:

    • UIImage *img = [UIImage imageNamed:[animal objectAtIndex:currentImage]];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2012-03-25
    相关资源
    最近更新 更多