fetchedResultsController fetchedObjects 返回一个 NSCFString 而不是 NSArray

Posted

技术标签:

【中文标题】fetchedResultsController fetchedObjects 返回一个 NSCFString 而不是 NSArray【英文标题】:fetchedResultsController fetchedObjects returning an NSCFString instead of NSArray 【发布时间】:2012-03-23 22:05:16 【问题描述】:

我正在尝试使用 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]);

我的 executeFetchRequest 在 fetchedResultsController 方法中的结果

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】:

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

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

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

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

看这个页面:String Format Specifiers

【讨论】:

非常感谢。就是这样。

以上是关于fetchedResultsController fetchedObjects 返回一个 NSCFString 而不是 NSArray的主要内容,如果未能解决你的问题,请参考以下文章

FetchedResultsController 中没有部分

如何将对象从 fetchedResultsController 到 Plist?

fetchedResultsController 对象的表视图部分

fetchedResultsController 和 Integer

将 fetchedResultsController 更改为 protected from private

将 fetchedResultsController 与 swift3 一起使用