访问实例会引发异常-NSDictionary NSArray



我是Objective-C的初学者,在字典和数组方面确实有点吃力。

我的目标是创建一个表视图,其中每个单元格都包含来自服务器的图片。图片的url来自一个JSON调用。我从苹果下载了一个名为"懒惰表视图"的例子,并试图将其与我在Standford iOS类中找到的例子合并。到目前为止,除了图片以外,一切都正常。一旦我尝试访问图标,Xcode就会抛出以下异常:

-[__NSCFDictionary Icon]: unrecognized selector sent to instance 0x10b845ff0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary Icon]: unrecognized selector sent to instance 0x10b845ff0'

有问题的代码如下所示:

@interface FlickrPhotosTVC : UITableViewController
@property (nonatomic, strong) NSArray *photos; 
@end
@interface ClRecord : NSObject
@property (nonatomic, strong) UIImage *Icon;
#define FLICKR_PHOTO_TITLE @"title"
ClRecord *clRecord = self.photos[indexPath.row];
cell.textLabel.text = [clRecord valueForKeyPath:FLICKR_PHOTO_TITLE]; // works
if (clRecord.Icon) NSLog(@"test"); // throws exeption

不知怎的,这似乎与类型有关。正在尝试访问数组,同时访问dictionary或类似项。我在stack上发现了一些相关的帖子,但到目前为止还无法解决。

谢谢你的帮助!

self.photos中存储的对象似乎是dictionary对象,而不是ClRecord对象。您得到的错误表明您正在对dictionary对象调用Icon方法。

检查照片数组的填充位置,确保在其中放置ClRecord对象,而不是字典对象。

您可以执行NSLog(@"My photos array is ---%@",self.photos);这将为您提供阵列中的确切内容。
案例1:如果您有一个dictionary对象,那么:-
 nbsp nbsp nbsp nbsp nbsp 你应该做NSDictionary *dict = self.photos[indexPath.row]; ClRecord *clRecord = [dict objectForKey:@"YourKeyForImage"];

案例2:如果你有你期望的图像,那么确保你导入了ClRecord头文件,你在指定的[indexPath.row]索引和其他必要的初始化中有非nil对象。
希望这能有所帮助。Link1

最新更新