2012-05-21 80 views
0

我对在iOS中开发很陌生。我正试图通过表格视图中的一个segue传递一些数据。下面是我打电话来传递数据:从对象中检索数据

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
if([segue.identifier isEqualToString:@"ShowContactDetails"]) { 
    ContactDetailsViewController *dvc = [segue destinationViewController]; 
    NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
    Contact *c = [jsonResults objectAtIndex:path.row]; 
    [dvc setSelectedContact:c]; 
    } 
} 

它通过罚款,当我做selectedContact一个NSLog,这就是我得到:

2012-05-21 15:15:42.410 Test[8352:f803] { 
category = Prospect; 
id = 19895; 
label = "John Doe"; 
} 

的问题是,我不不知道如何分别访问这些项目。我试图打电话给objectForKey,但是我收到一个错误消息。

+0

什么是类? 'NSLog(“%@”,NSStringFromClass(c))' – bshirley

+0

你得到的错误究竟是什么? – Alladinian

回答

2

从NSLog的输出中,selectedContact看起来像是一个NSDictionary对象。尝试使用[selectedContact valueForKey]而不是[selectedContact objectForKey]。

例如:

NSLog(@"Name: %@", [selectedContact valueForKey:@"label"]); 
+0

你不能确定这是一本字典,因为'NSLog'就是这样打印的。例如,它可以是一个带有重写的'description'方法的自定义类,它将所有属性收集到字典中并将其返回以进行日志记录。 – Alladinian

+0

是的,它是一个字典对象。它是从NSJSONSerialization返回的数据。我会在明天工作时尝试valueForKey,看看它是否有效。 – Keith

+0

工作,谢谢! – Keith