Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在核心数据中使用id获取_Iphone_Objective C_Ios_Core Data_Entities - Fatal编程技术网

Iphone 在核心数据中使用id获取

Iphone 在核心数据中使用id获取,iphone,objective-c,ios,core-data,entities,Iphone,Objective C,Ios,Core Data,Entities,我的应用程序中有一个核心数据。其中一个实体是详细信息Details有两个名为guid的属性,另一个是Details。这两个属性都包含很多值。在获取时,我必须获取相应guid的详细信息。我可以用guid获取保存在其他属性中的值吗?如何连接这两个属性?这就是我将值保存到2个属性的方式 ReaderAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *conte

我的应用程序中有一个核心数据。其中一个实体是
详细信息
Details
有两个名为
guid
的属性,另一个是
Details
。这两个属性都包含很多值。在获取时,我必须获取相应
guid
详细信息。我可以用guid获取保存在其他属性中的值吗?如何连接这两个属性?这就是我将值保存到2个属性的方式

ReaderAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSManagedObject *object;
object = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:context];

[object setValue: [[activity objectForKey:@"category_guid"]stringValue]forKey:@"guid"];
[object setValue:[activity objectForKey:@"details"] forKey:@"details"];

NSError *error;            
if (![context save:&error]) {

    NSLog(@"Error save base");
}
else {

    NSLog(@"saved");                
}

我不确定我是否理解您的问题,但如果您想基于
guid
选择特定的
详细信息
对象,可以使用
NSPredicate
创建
NSFetchRequest
,如下所示:

NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Details" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"guid == %@", yourGuid]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if([results count] > 0) {

    // the object you are looking for
    NSManagedObject* grabbedDetail = [results objectAtIndex:0];

    NSString* grabbedDetails = [grabbedDetail valueForKey:@"details"];

    // here you have grabbed details attribute
    // I suppose it's of type NSSString... 
}

// [request release]; if you don't use ARC
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Details" inManagedObjectContext:moc]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if([results count] > 0) {

    for(NSManagedObject* obj in results) {

        NSLog(@"obj -> %@", obj)
    }
}

// [request release]; if you don't use ARC
关于你的问题,你能解释一下这些是什么意思吗

我可以用guid获取保存在其他属性中的值吗? 如何连接这两个属性

希望有帮助

编辑:

如果
results
为零,则可以首先检索不带谓词的对象。因此,请执行以下操作:

NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Details" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"guid == %@", yourGuid]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if([results count] > 0) {

    // the object you are looking for
    NSManagedObject* grabbedDetail = [results objectAtIndex:0];

    NSString* grabbedDetails = [grabbedDetail valueForKey:@"details"];

    // here you have grabbed details attribute
    // I suppose it's of type NSSString... 
}

// [request release]; if you don't use ARC
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Details" inManagedObjectContext:moc]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if([results count] > 0) {

    for(NSManagedObject* obj in results) {

        NSLog(@"obj -> %@", obj)
    }
}

// [request release]; if you don't use ARC
如果您没有日志,则表示您没有数据!!相反,使用硬编码的
guid
值设置谓词,如下所示:

[request setPredicate:[NSPredicate predicateWithFormat:@"guid == %@", @"12345"]];
您需要确保
guid
存在。

我不知道问题出在哪里,但我使用的查询是正确的,没有任何细节,我无法理解到底发生了什么


干杯。

我刚按你说的做了尝试。但是[结果计数]越来越高,因为你需要提供一些关于你的模型的信息。
guid
的类型是什么?如果查询返回0,则表示没有对象具有您指定的
guid
。首先尝试使用硬编码的
guid
(确保它存在),然后看看会发生什么。e、 g.
[NSPredicate predicateWithFormat:@“guid==%@”,“12345”]
@alpz删除谓词并打印结果。您能够检索任何对象吗?我对答案进行了编辑,以符合这一点。