对于熟悉 CoreData 的 iPhone 开发人员来说,这可能更容易,但我不是而且真的很想知道如何在关键期限内正确访问我的数据。
这就是我的核心数据一旦设置为 anNSMutableArray
并使用 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 中看到有 3 个 Questions
- 问题=“什么是……?
- 问题=\“……怎么样?
- 问题 = \"……在哪里?
它还表明关系中有一个策略部分:
strategy = \"<relationship fault: 0x826c430 'strategy'>\
我需要做的是访问此策略并向其添加一个 NSString 类型的对象。
一旦分配,我需要将策略作为策略拉到 NSMutableArray 以推送到 UITableView。
我知道这是很多东西,但对于 Apple Guru 来说,这可能听起来像是蛋糕和冰淇淋。
如果需要更多信息来回答,请在评论中告诉我.... 一位伟大的 StackOverflow 成员曾经告诉我
“有时最困难的部分是找到提出问题的正确方法。”
任何帮助深表感谢!