【发布时间】:2013-07-11 16:45:10
【问题描述】:
这对于熟悉 CoreData 但我不熟悉 CoreData 的 iPhone 开发人员来说可能更容易,而且我真的很想知道如何在关键期限内正确访问我的数据。
这就是我的核心数据一旦设置为 NSMutableArray 并使用 NSLog 提取的内容
Questions List KM: (
"<Question: 0x8589630> (entity: Question; id: 0x8588820 <x-coredata://FDC76465-4884-473A-B46A-3451BCB74F5C/Question/p1> ; data: {\n orderID = 0;\n project = \"0x818e300 <x-coredata://FDC76465-4884-473A-B46A-3451BCB74F5C/Project/p1>\";\n question = \"What is ...? \";\n strategy = \"<relationship fault: 0x8264930 'strategy'>\";\n})",
"<Question: 0x85898d0> (entity: Question; id: 0x8588830 <x-coredata://FDC76465-4884-473A-B46A-3451BCB74F5C/Question/p2> ; data: {\n orderID = 1;\n project = \"0x818e300 <x-coredata://FDC76465-4884-473A-B46A-3451BCB74F5C/Project/p1>\";\n question = \"How is ...? \";\n strategy = \"<relationship fault: 0x826fa60 'strategy'>\";\n})",
"<Question: 0x8589930> (entity: Question; id: 0x8588840 <x-coredata://FDC76465-4884-473A-B46A-3451BCB74F5C/Question/p3> ; data: {\n orderID = 2;\n project = \"0x818e300 <x-coredata://FDC76465-4884-473A-B46A-3451BCB74F5C/Project/p1>\";\n question = \"Where is ...? \";\n strategy = \"<relationship fault: 0x826c430 'strategy'>\";\n})"
)
我可以很容易地将它拉到一个数组中:
// fetch and sort questions
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"orderID" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
NSLog(@"No Results in Questions Array.");
}
[self setQuestionsList:mutableFetchResults];
这里是 Question.h 或问题定义:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Project, Strategy;
@interface Question : NSManagedObject
@property (nonatomic, retain) NSNumber * orderID;
@property (nonatomic, retain) NSString * question;
@property (nonatomic, retain) Project *project;
@property (nonatomic, retain) NSSet *strategy;
@end
@interface Question (CoreDataGeneratedAccessors)
- (void)addStrategyObject:(Strategy *)value;
- (void)removeStrategyObject:(Strategy *)value;
- (void)addStrategy:(NSSet *)values;
- (void)removeStrategy:(NSSet *)values;
@end
这里是 Strategy.h 的定义:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Question;
@interface Strategy : NSManagedObject
@property (nonatomic, retain) NSString * strategy;
@property (nonatomic, retain) Question *question;
@end
在数据的NsLog中可以看到有三个问题
- question = \"什么是...?
- question = \"...怎么样?
- question = \"……在哪里?
它还表明关系中有一个策略部分:
strategy = \"<relationship fault: 0x826c430 'strategy'>\
我需要做的是访问这个策略并向它添加一个 NSString 类型的对象。
一旦分配,我需要将策略作为策略拉到 NSMutableArray 以推送到 UITableView。
我知道这有很多东西需要吸收,但对于 Apple Guru 来说,这可能听起来像是蛋糕和冰淇淋。
如果需要更多信息来回答,请在 cmets 中告诉我....曾经有一位伟大的 StackOverflow 成员告诉我
“有时最困难的部分是找到正确的方法来询问 问题。”
非常感谢任何帮助!
【问题讨论】:
-
显示所有实体和关系的定义会有所帮助。
-
@MartinR 我怎样才能找到这些?我可以看到表格....项目中的另一个开发人员最初构建了 coredata,我必须插入它。
-
每个实体可能都有一个 .h 文件,例如“Question.h”。你可以展示这些。
-
@MartinR 已更新,谢谢!
-
Strategy.h 可能也需要。 “将单个对象添加到类型 NSString 的策略中”是什么意思?
标签: objective-c ipad core-data nsmutablearray