我遇到一个应用程序崩溃的问题,但有以下异常:

ABC [1936:c07] *由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ valueForUndefinedKey:]:此类不适用于密钥ID的密钥值编码。”

带有此异常的奇怪问题是,使用iOS5时不会发生。请在下面查看发生异常的代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

if ((self.sectionInfoArray == nil) ||
    ([self.sectionInfoArray count] != [self numberOfSectionsInTableView:self.tableView]))
{
    NSMutableArray *infoArray = [[NSMutableArray alloc] init];

    for (Tour *tour in self.tours)
    {
        SectionInfo *sectionInfo = [[SectionInfo alloc] init];
        sectionInfo.tour = tour;
        sectionInfo.open = NO;

        NSLog(@"Tour Details Count %@", [[tour tourDetails] objectAtIndex:0]);

        NSNumber *defaultRowHeight = [NSNumber numberWithInteger:DEFAULT_ROW_HEIGHT];
        NSInteger countOfQuotations = [[sectionInfo.tour tourDetails] count];

        for (NSInteger i = 0; i < countOfQuotations; i++)
        {
            [sectionInfo insertObject:defaultRowHeight inRowHeightsAtIndex:i];
        }

        [infoArray addObject:sectionInfo];
    }

    self.sectionInfoArray = infoArray;
}

}

是否会由于我在Tour类中定义了Fetched属性而获取了TourDetail类数组的原因而导致了该异常。请在下面的两个类中查看实现代码:
    #import "Tour.h"
#import "TourDetail.h"

@implementation Tour

@dynamic background_url;
@dynamic id;
@dynamic summary;
@dynamic title;
@dynamic tour_tourdetail;

@dynamic tourDetails;

@end

#import "TourDetail.h"


#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class TourDetail;

@interface Tour : NSManagedObject

@property (nonatomic, retain) NSString * background_url;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * summary;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) TourDetail *tour_tourdetail;

@property (nonatomic, retain) NSArray *tourDetails;

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface TourDetail : NSManagedObject

@property (nonatomic, retain) NSString * audiofile;
@property (nonatomic, retain) NSString * detail;
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSNumber * lattitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * tour_id;
@property (nonatomic, retain) NSManagedObject *tourdetail_tour;

@end



@implementation TourDetail

@dynamic audiofile;
@dynamic detail;
@dynamic id;
@dynamic lattitude;
@dynamic longitude;
@dynamic title;
@dynamic tour_id;
@dynamic tourdetail_tour;

@end

任何与此问题的帮助将不胜感激。由于我对如何解决此问题不知所措。

谢谢,
麦可

更新:

当我删除Fetched Property时,iOS6不会发生该异常。请参阅以下我配置的谓词:

获取的属性tourDetails谓词tour_id == $ FETCH_SOURCE.id

您能看到我对该谓词的设置做错了什么吗?我的目标是使用它,以便为每个对Tour表中的id列进行计算的tour_id返回一个TourDetail对象的数组。

更新:

我已经能够诊断出由于谓词而引发了异常,因为当我分别调用两个表时,没有引发异常。我创建的谓词有任何问题吗?

请参阅下面的代码,显示我如何从Core Data DB中检索对象:
- (void)viewDidLoad
{
    [super viewDidLoad];

    [DrivingToursContent setupStaticData];

    self.tableView.sectionHeaderHeight = HEADER_HEIGHT;
    _openSectionIndex = NSNotFound;

    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"custombackground.ptoung"]];

    self.managedObjectContext = [[BaseCoreDataController sharedInstance] newManagedObjectContext];

    [self loadRecordsFromCoreData];

    [self loadRecordsFromCoreDataForTourDetail];

    NSLog(@"Tour Detail array count: %d", [self.toursTest count]);

    // Do any additional setup after loading the view.
}

- (void)loadRecordsFromCoreData {
    [self.managedObjectContext performBlockAndWait:^{
        [self.managedObjectContext reset];
        NSError *error = nil;
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Tour class])];
        [request setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES]]];
        self.tours = [self.managedObjectContext executeFetchRequest:request error:&error];
    }];
}

更新:

问题的根源肯定来自于我为Fetched属性定义的谓词,但是可以建议我如何编写谓词以链接两个表。当我编写谓词tour_id == 0并直接引用我知道存在的id时,提取的属性可以正常工作。但是,当我使用$ FETCH_SOURCE.id时,会抛出键值编码异常。您使用什么属性来引用要链接到的表?

非常感谢您为此提供的所有帮助。

谢谢,
麦可

最佳答案

没有看到您的代码,您正在在iOS6中弃用的类上调用方法,因此导致崩溃。

08-06 04:08