fetchedResultsController fetchedObjects返回NSCFString而不是NSArray

时间:2012-03-23 22:05:16

标签: ios core-data exc-bad-access nsfetchedresultscontroller

我正在尝试使用fetchedResultsController设置带有Core Data的UITableView。

查询本身显示正常。我通过executeFetchRequest语句测试它。

我调用performFetch,没有错误。

当我尝试访问fetchedObjects时,我得到一个EXC_BAD_ACCESS。

查看Xcode中返回的变量,它似乎返回一个_NSCFString而不是NSArray。

我在Google上搜索了所有可能的内容,找到了许多此代码的示例,我无法弄清楚我做错了什么。我似乎找不到同样问题的例子。

以下是代码部分:

.h文件

@interface MainViewController : UIViewController <NSFetchedResultsControllerDelegate> {

NSFetchedResultsController *_fetchedResultsController;
}

@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;

.m文件

@synthesize fetchedResultsController = _fetchedResultsController;

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity.

 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Ingredient"];
 request.predicate = [NSPredicate predicateWithFormat:@"recipe.name = %@",[self theRecipe].name];
 NSSortDescriptor *aSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"sequenceNumber" ascending:YES];
 request.sortDescriptors = [NSArray arrayWithObject:aSortDescriptor];

NSError *error = nil;
NSArray *ingredients = [[self managedObjectContext] executeFetchRequest:request error:&error];

if (!ingredients) {
    NSLog(@"** No Ingredients found");
} else if ([ingredients count] >= 0) {
    NSLog(@"**%d Ingredients found", [ingredients count]);
} 

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:@"myCache"];
aFetchedResultsController.delegate = self;
[self setFetchedResultsController:aFetchedResultsController];

return _fetchedResultsController;
}    

内部ViewDidLoad

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

NSLog(@"fetchRequest = .... %@",[[self fetchedResultsController] fetchRequest]);
NSLog(@"fetchedObjects = .... %@",[[self fetchedResultsController] fetchedObjects]);
NSLog(@"lastObject = .... %@",[[[self fetchedResultsController] fetchedObjects] lastObject]);
Ingredient *test = [[[self fetchedResultsController] fetchedObjects] lastObject];
NSLog(@"test = %@", test.ingredient);
NSArray *fetchedObjects = [[self fetchedResultsController] fetchedObjects];  // EXC_BAD_ACCESS hit here
NSLog(@".... ingredients count = %@", [fetchedObjects count]);

来自fetchedResultsController方法内的executeFetchRequest的结果

2012-03-24 08:46:23.171 My App[103:707] **8 Ingredients found

这是我得到的日志输出:

2012-03-24 08:46:23.186 My App[103:707] fetchRequest = .... <NSFetchRequest: 0x2c5180> (entity: Ingredient; predicate: (recipe.name == "Lasagne"); sortDescriptors: ((
"(sequenceNumber, ascending, compare:)"
)); type: NSManagedObjectResultType; )

2012-03-24 08:46:23.192 My App[103:707] fetchedObjects = .... (
"<Ingredient: 0xee23e80> (entity: Ingredient; id: 0xee23100 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p9> ; data: <fault>)",
"<Ingredient: 0xee24120> (entity: Ingredient; id: 0xee23160 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p3> ; data: <fault>)",
"<Ingredient: 0xeed59a0> (entity: Ingredient; id: 0xee23170 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p7> ; data: <fault>)",
"<Ingredient: 0xeed5a00> (entity: Ingredient; id: 0xee23180 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p2> ; data: <fault>)",
"<Ingredient: 0xee243c0> (entity: Ingredient; id: 0xee23190 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p5> ; data: <fault>)",
"<Ingredient: 0xee24410> (entity: Ingredient; id: 0xee231a0 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p1> ; data: <fault>)",
"<Ingredient: 0xee24460> (entity: Ingredient; id: 0xee231b0 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p6> ; data: <fault>)",
"<Ingredient: 0xee244c0> (entity: Ingredient; id: 0xee231c0 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p8> ; data: <fault>)"

这显示了8条结果记录。这就是我所期待的。

2012-03-24 08:46:23.195 My App[103:707] lastObject = .... <Ingredient: 0xee244c0> (entity: Ingredient; id: 0xee231c0 <x-coredata://E3F4AFFB-B14F-4E94-80B1-F6101F92FAAD/Ingredient/p8> ; data: <fault>)

2012-03-24 08:46:23.198 My App[103:707] test = Salt

这也是我期待的价值。

(gdb) 

那么为什么fetchedObjects的结果不是NSArray,我该怎么做呢?

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我认为你的代码在这一行崩溃了:

NSLog(@".... ingredients count = %@", [fetchedObjects count]);  

%@指向一个对象。它必须是%i[fetchedObjects count]返回不是对象的NSUInteger。所以该行应为:

NSLog(@".... ingredients count = %i", [fetchedObjects count]);

查看此页面:String Format Specifiers