gpt4 book ai didi

ios - 尝试从多个实体中获取记录

转载 作者:行者123 更新时间:2023-11-29 11:43:39 24 4
gpt4 key购买 nike

我将尝试与具有核心数据的多个实体合作。StudentDetail 两个实体名称中的核心数据都是反比关系。关系名称是 Student -> Detail:detail Detail -> Student:student。尝试从两个实体中获取记录并在 TableView 中显示。尝试多个代码但无法获得结果。

第一个代码:

Detail *d;
NSMutableArray *arrObj = [[NSMutableArray alloc]init];
for(Student *tblObj in [d student]){
[arrObj addObject:tblObj];
}
NSLog(@"Your records related with tableA = %@",arrObj);

无法加载数组中的数据。

第二个代码:

NSArray* fetchResults = [_mainContext executeFetchRequest:fetchRequest error:nil];
for (int i; i > 1; i++) {
Student *tableAObject = fetchResults[i];
NSString * itemcode = tableAObject.detail.email;
NSLog(@"%@",itemcode);
}

无法显示记录。

第三种方式:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *aEntity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:_mainContext];
[fetchRequest setEntity:aEntity];

NSString *relationshipKeyPath = @"detail"; // Set this to the name of the relationship on "SP" that points to the "Code" objects;
NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath];
[fetchRequest setRelationshipKeyPathsForPrefetching:keyPaths];

NSMutableArray *arrObj = [[NSMutableArray alloc]init];
for(Student *spObj in arrObj)
{
NSLog(@"description is %@",spObj.name);
Detail *codObj = spObj.detail;
NSLog(@"it has value %@",codObj);
NSLog(@" unique name is %@",codObj.email);
}

在核心数据中插入数据的代码:

- (IBAction)submitData:(id)sender {

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate manageObjectContext];

Student *student = (Student *)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

student.name = _nameText.text;
student.number = _noText.text;
student.study = _studyText.text;

Detail *detail = (Detail *)[NSEntityDescription insertNewObjectForEntityForName:@"Detail" inManagedObjectContext:context];

detail.number = _noText.text;
detail.email = _emailText.text;
detail.address = _addressText.text;
detail.contact = _contactText.text;

student.detail = detail;
detail.student = student;
NSError *error = nil;
[context save:&error];

if (![context save:&error]) {
NSLog(@"error in adding data %@, %@", error, [error userInfo]);
abort();
}
}

如果您需要更多详细信息,请告诉我。谢谢。

最佳答案

您尝试过的前两种方法都行不通,因为没有与从核心数据中获取数据相关的代码。第三种方式的问题是你没有执行你的获取请求。以下是从核心数据中获取数据的方法。

NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
NSError *error = nil;
NSArray *students = [context executeFetchRequest:req error:&error];

for (Student *stdObj in students)
{
//Student object
NSLog(@"Name %@",stdObj.name);
NSLog(@"Number %@",stdObj.number);

//Detail object related to student
Detail *detailObj = stdObj.detail;
NSLog(@"Email %@",detailObj.email);
NSLog(@"Address %@",detailObj.address);
}

关于ios - 尝试从多个实体中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com