gpt4 book ai didi

iphone - NSManagedObject 异常 “this class is not key value coding-compliant” 并在 ios 6 中使应用程序崩溃,但适用于 ios 5

转载 作者:可可西里 更新时间:2023-11-01 03:21:26 25 4
gpt4 key购买 nike

我的应用程序因以下异常而崩溃:

ABC[1936:c07] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<_NSObjectID_48_0 0xb63e310> valueForUndefinedKey:]:此类不符合键值编码 key 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 Property 时,iOS6 不会出现异常。请查看我在下面配置的谓词:

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

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

更新:

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

请看下面的代码,展示了我如何从核心数据数据库中检索对象:

- (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];
}];
}

更新:

问题的根源肯定来 self 为 Fetched 属性定义的谓词,但是你能建议我应该如何编写谓词以在 2 个表之间进行链接吗?当我编写谓词 tour_id == 0 并直接引用我知道存在的 id 时,获取的属性可以正常工作。但是当我使用 $FETCH_SOURCE.id 时,会抛出键值编码异常。您使用什么属性来引用您希望链接到的表?

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

谢谢,迈克尔

最佳答案

没有看到你的代码,你正在调用一个在 iOS6 中被弃用的类的方法,因此崩溃。

关于iphone - NSManagedObject 异常 “this class is not key value coding-compliant” 并在 ios 6 中使应用程序崩溃,但适用于 ios 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14759094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com