我遇到了一个问题,即我的应用程序崩溃并出现以下异常:
ABC[1936:c07] *由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<_NSObjectID_48_0 0xb63e310> 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 中定义了一个获取的属性而导致这个异常,该类获取了一个 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 属性时,iOS6 不会发生异常。请参阅我在下面配置的谓词:
获取的属性 tourDetails 谓词 tour_id == $FETCH_SOURCE.id
你能看到我在设置这个谓词时做错了什么吗?我的目标是使用它,以便我可以为每个 tour_id 返回一个 TourDetail 对象数组,该对象对 Tour 表中的 id 列进行数学运算。
更新:
我已经能够诊断出由于谓词而引发了异常,因为当我分别调用两个表时,没有引发异常。你能看出我创建的谓词有什么问题吗?
请参阅下面的代码,显示我如何从 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 时,会引发键值编码异常。您使用什么属性来引用您希望链接到的表?
非常感谢您对此提供的所有帮助。
谢谢,迈克尔