从plist setObjectForKey加载图像:对象不能是nil(key:image)错误

时间:2014-03-16 22:05:18

标签: uitableview

拜托,我可以帮助我解决在Xcode 5中练习IOS应用程序时收到的这个错误

2014-03-16 14:10:44.072 TabCreator[563:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: image).

在images.xcassets中加载的图像和我创建了一个plist文件,并试图用plist中的数组填充UITableView:

<plist version="1.0">
<array>
    <dict>
        <key>pic</key>
        <string>skyline.png</string>
        <key>name</key>
        <string>Skyline</string>
        <key>datecreated</key>
        <date>1975-06-03T23:00:00Z</date>
    </dict>
    <dict>
        <key>pic</key>
        <string>hummer.png</string>
        <key>name</key>
        <string>Hummer</string>
        <key>datecreated</key>
        <date>1963-12-18T00:00:00Z</date>
    </dict>

    </array>
</plist>

和我的HomeviewController.m文件是:

@interface HomeViewController ()
@property   (nonatomic,strong)NSMutableArray *functions;
@end

@implementation FTHomeViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

        NSString *plistPath =[[NSBundle mainBundle]pathForResource:@"functions" ofType:@"plist"];
        NSArray *nonMutableFunctions = [NSArray arrayWithContentsOfFile:plistPath];

        self.functions = [NSMutableArray array];

        NSMutableDictionary *function;
        NSDictionary *dictionary;
        NSString *name;
        NSString *pic;
        UIImage *image;
        NSDate *datecreated;

        for (int i=0; i<[nonMutableFunctions count]; i++) {
            dictionary = [nonMutableFunctions objectAtIndex:i];
            name=dictionary[@"name"];
            pic=dictionary[@"pic"];
            image=[UIImage imageNamed:pic];
            datecreated =dictionary[@"datecreated"];
            function = [NSMutableDictionary dictionary];

            function[@"name"]=name;
            function[@"image"]=image;
            function[@"datecreated"]=datecreated;

            [self.functions addObject:function];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"];

    NSMutableDictionary *function=self.functions[indexPath.row];

    NSString *name =function[@"name"];
    NSDate *datecreated = function[@"datecreated"];
    UIImage *image = function[@"image"];

    cell.textLabel.text = name;
    cell.detailTextLabel.text = datecreated.description;
    cell.imageView.image = image;

    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.functions count];
}

//Deselect selected row when selected

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end

如果有人可以帮助我,我会非常感激。感谢

1 个答案:

答案 0 :(得分:0)

当我在预览中从jpg导出到png时,只是简单省略了png扩展名。不得不留下扩展名,因为图表文件包含完整的文件名,包括扩展名。