NSManagedObject异常"此类不符合键值编码"并在ios 6中崩溃应用程序但适用于ios 5

Mic*_*ell 8 iphone core-data ios

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

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;
}
Run Code Online (Sandbox Code Playgroud)

}

是否会导致此异常,因为我在类Tour中定义了一个获取TourDetail类数组的Fetched属性.请参阅以下两个类的实现代码:

    #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
Run Code Online (Sandbox Code Playgroud)

任何有关此问题的帮助将不胜感激.因为我不知道如何解决这个问题.

谢谢,迈克尔

更新:

当我删除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];
    }];
}
Run Code Online (Sandbox Code Playgroud)

更新:

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

真的很感谢你对此的所有帮助.

谢谢,迈克尔

And*_* G. 0

在没有看到代码的情况下,您正在调用 iOS6 中已弃用的类上的方法,从而导致崩溃。

  • **不符合什么键值?** 如果某个对象不符合键值,该错误会告诉您它不符合哪个键。 (4认同)