首页 > 代码库 > 将从数组中取到的字符串赋值给了UIImage导致的错误

将从数组中取到的字符串赋值给了UIImage导致的错误

  Terminating app due to uncaught exception ‘NSInvalidArgumentException‘, reason: ‘-[__NSCFConstantString size]: unrecognized selector sent to instance 0xa500‘

      NSArray * imageArr = @[@"tabbar_limitfree",
                           @"tabbar_reduceprice",
                           @"tabbar_appfree",
                           @"tabbar_subject",
                           @"tabbar_rank"];

   [item setImage:imageArr[i] forState:UIControlStateNormal]; //这行代码具有迷惑性,titleArr[i]是取出来的数据类型是 id ,实际上取到值是NSString类型的。因为OC是运行时语言,所以编译时,编译器会觉得它是对的,但是到了运行的时候,发现它并没有想要的方法,所以报了经典错误 unrecognized selector sent to instance。

  改正后的代码:

    [item setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];

 

  

  

将从数组中取到的字符串赋值给了UIImage导致的错误