我有两个实体 - “教师”和“主题”,以及2个控制器,AddSubject和Subject manager。实体“主体”与“教师”有关系。在我AddSubjectController我增加了主题从UIPickerView选择老师通过核心数据中的关系获得价值
Subjects *subject = [NSEntityDescription insertNewObjectForEntityForName:@"Subjects" inManagedObjectContext:self.managedObjectContext];
int selectedRow = [teacherPickerView selectedRowInComponent:0];
subject.name = nameOfSubjectTextField.text;
subject.article = notesTextView.text;
subject.teacher = [teacherForSubjectArray objectAtIndex:selectedRow];
teacherForSubjectArray是从核心数据
- (NSArray *)takeTeacherForSubject {
managedObjectContext = [AppDelegate sharedDelegate].managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *description = [NSEntityDescription entityForName:@"Teacher" inManagedObjectContext:
managedObjectContext];
[request setEntity:description];
NSError *requestError = nil;
teacherForSubjectArray = [[self.managedObjectContext executeFetchRequest:request error:&requestError] mutableCopy];
if (requestError) {
NSLog(@"%@" , [requestError localizedDescription]);
}
return teacherForSubjectArray;
}
然后在我的SubjectManager我试图表现出UITable这个数据与对象的数组。认为,从核心数据服用之前
- (NSArray *)takeSubjectFromCoreData {
managedObjectContext = [AppDelegate sharedDelegate].managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *description = [NSEntityDescription entityForName:@"Subjects" inManagedObjectContext:
managedObjectContext];
[request setEntity:description];
NSError *requestError = nil;
subjectsResultArray = [[self.managedObjectContext executeFetchRequest:request error:&requestError] mutableCopy];
if (requestError) {
NSLog(@"%@" , [requestError localizedDescription]);
}
NSLog(@"%@", subjectsResultArray);
return subjectsResultArray;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Subjects *objectSubject = [subjectsResultArray objectAtIndex:(long)indexPath.row];
UITableViewCell *cell = [subjectsTableView dequeueReusableCellWithIdentifier:@"subjectCell" forIndexPath:indexPath];
NSString *subjectName = [NSString stringWithFormat:@"%@", [objectSubject valueForKey:@"name"]];
NSString *teacherOfSubject = [NSString stringWithFormat:@"%@", objectSubject.teacher];
cell.textLabel.text = subjectName;
cell.detailTextLabel.text = teacherOfSubject;
return cell;
}
但与一个老师允许问题rOfSubject,我得到不是当前值,而是一个路径,或NSmanagedObject而不是价值,我需要,像
0x7a6806d0 X-coredata:// 6DB0CDAF-5C8C-4128-BB43-D0EC10EA41D6 /教师/ P2。
我知道答案应该很简单,但我厌倦了试图自己解决它。
如果我的问题不够清楚,请说出来。 – SpencerReid 2014-10-31 16:48:25