Ios fetchedResultsController获取返回NSCFString而不是NSArray的对象
我正在尝试使用fetchedResultsController使用核心数据设置UITableView 查询本身看起来正常。我通过executeFetchRequest语句测试它 我称之为performFetch,没有错误 当我试图访问获取的对象时,我得到了一个EXC\u BAD\u访问权限 查看Xcode中返回的变量,它似乎返回了一个_NSCFString而不是NSArray 我在谷歌上搜索了所有我能找到的,找到了很多这段代码的例子,但我不知道我做错了什么。我似乎找不到同样问题的例子 以下是代码部分: .h文件Ios fetchedResultsController获取返回NSCFString而不是NSArray的对象,ios,core-data,exc-bad-access,nsfetchedresultscontroller,Ios,Core Data,Exc Bad Access,Nsfetchedresultscontroller,我正在尝试使用fetchedResultsController使用核心数据设置UITableView 查询本身看起来正常。我通过executeFetchRequest语句测试它 我称之为performFetch,没有错误 当我试图访问获取的对象时,我得到了一个EXC\u BAD\u访问权限 查看Xcode中返回的变量,它似乎返回了一个_NSCFString而不是NSArray 我在谷歌上搜索了所有我能找到的,找到了很多这段代码的例子,但我不知道我做错了什么。我似乎找不到同样问题的例子 以下是代码
@interface MainViewController : UIViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *_fetchedResultsController;
}
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
内视图加载
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>)"
那么,为什么FetchedObject的结果不是NSArray,如何使其成为NSArray呢
感谢您的帮助。我认为您的代码在这一行中崩溃了:
NSLog(@".... ingredients count = %@", [fetchedObjects count]);
%@
指向一个对象。它必须是%i
[fetchedObjects count]
返回一个非对象的整数。所以这行应该是:
NSLog(@".... ingredients count = %i", [fetchedObjects count]);
请看本页:非常感谢。就这样。
(gdb)
NSLog(@".... ingredients count = %@", [fetchedObjects count]);
NSLog(@".... ingredients count = %i", [fetchedObjects count]);