我遇到了一些我最终想到的东西,但认为可能有一种更有效的方法来实现它.
我有一个对象(一个采用MKAnnotation协议的NSObject),它有许多属性(标题,副标题,纬度,经度,信息等).我需要能够将此对象传递给另一个对象,该对象希望使用objectForKey方法从中提取信息,作为NSDictionary(因为这是从另一个视图控制器获取的内容).
我最终做的是创建一个新的NSMutableDictionary并在其上使用setObject:forKey来传输每条重要信息,然后我只传递新创建的字典.
有没有更简单的方法来做到这一点?
这是相关的代码:
// sender contains a custom map annotation that has extra properties...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetailFromMap"])
{
DetailViewController *dest =[segue destinationViewController];
//make a dictionary from annotaion to pass info
NSMutableDictionary *myValues =[[NSMutableDictionary alloc] init];
//fill with the relevant info
[myValues setObject:[sender title] forKey:@"title"] ;
[myValues setObject:[sender subtitle] forKey:@"subtitle"];
[myValues setObject:[sender info] forKey:@"info"];
[myValues setObject:[sender pic] forKey:@"pic"];
[myValues setObject:[sender latitude] forKey:@"latitude"];
[myValues setObject:[sender longitude] forKey:@"longitude"];
//pass values
dest.curLoc = myValues; …Run Code Online (Sandbox Code Playgroud)