0

我的应用程序的调试器中不断出现错误说,

2013-06-23 16:07:15.826 集合视图食谱 [5681:c07]-[NSManagedObject 长度]:无法识别的选择器发送到实例 0x9495280 2013-06-23 16:07:15.827 集合视图食谱 [5681:c07] * 终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“-[NSManagedObject 长度]:无法识别的选择器发送到实例 0x9495280”*First throw call stack: (0x26ac012 0x1517e7e 0x27374bd 0x269bbbc 0x269b94e 0x2b11c4 0x16d80a 0x4464 0x64f2da 0x6508f4 0x652b91 0x19c2dd 0x152b6b0 0x18eefc0 0x18e333c 0x18eeeaf 0x23b2bd 0x183b56 0x18266f 0x182589 0x1817e4 0x18161e 0x1823d9 0x1852d2 0x22f99c 0x17c574 0x17c76f 0x17c905 0x185917 0x14996c 0x14a94b 0x15bcb5 0x15cbeb 0x14e698 0x2c06df9 0x2c06ad0 0x2621bf5 0x2621962 0x2652bb6 0x2651f44 0x2651e1b 0x14a17a 0x14bffc 0x1e9d 0x1dc5) libc++abi.dylib: 终止调用抛出异常

在我的应用程序委托中,如果检查应用程序是否是第一次启动,如果是,然后我将几个图像路径添加到核心数据结构中。在 ApplicationDidFinishLaunchingWithOptions 下的 AppDelegate.m 中,

  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    // app already launched
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // This is the first launch ever
        NSArray *mainDishImages = [NSArray arrayWithObjects:@"egg_benedict.jpg", @"full_breakfast.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", nil];
        NSArray *drinkDessertImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"green_tea.jpg", @"starbucks_coffee.jpg", @"white_chocolate_donut.jpg", nil];
    for (NSString *imagePath in mainDishImages) {


    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:context];
        [newRecipe setValue:imagePath forKey:@"imageFilePath"];
    }
    for (NSString *imagePath in drinkDessertImages) {
        NSManagedObjectContext *context = [self managedObjectContext];
        NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Deserts" inManagedObjectContext:context];
        [newRecipe setValue:imagePath forKey:@"imageFilePath"];
    }
}

我在我的 collectionViewController 中访问该数据,我访问该数据。

- (NSManagedObjectContext *)managedObjectContext{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return  context;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Deserts"];
    deserts = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSFetchRequest *fetchRequestTwo = [[NSFetchRequest alloc] initWithEntityName:@"Recipe"];
    meals = [[managedObjectContext executeFetchRequest:fetchRequestTwo error:nil] mutableCopy];
    recipeImages = [NSArray arrayWithObjects:meals, deserts, nil];
    [self.collectionView reloadData];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Deserts"];
    deserts = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSFetchRequest *fetchRequestTwo = [[NSFetchRequest alloc] initWithEntityName:@"Recipe"];
    meals = [[managedObjectContext executeFetchRequest:fetchRequestTwo error:nil] mutableCopy];
    recipeImages = [NSArray arrayWithObjects:meals, deserts, nil];
    UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    collectionViewLayout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    self.navigationController.navigationBar.translucent = YES;
    self.collectionView.contentInset = (UIEdgeInsetsMake(44, 0, 0, 0));
    selectedRecipes = [NSMutableArray array];

}

根据 crashalytics 的说法,错误在它说的那一行

recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]];

在方法中

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipeImages[indexPath.section] objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame"]];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame-selected.png"]];
return cell;
}

我希望你能帮忙。提前致谢!

4

1 回答 1

0

UIImage 方法 imageNamed 将 NSString 作为参数,但您将 NSManagedObject 传递给它。

您应该首先从托管对象获取图像路径。尝试这个:

id managedObject = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
NSString* imagePath = [managedObject valueForKey:@"imageFilePath"];
recipeImageView.image = [UIImage imageNamed:imagePath];
于 2013-06-23T20:42:59.503 回答