OBJ-C简单的方法来从具有属性的NSObject转换为NSDictionary? [英] Obj-C easy method to convert from NSObject with properties to NSDictionary?

查看:90
本文介绍了OBJ-C简单的方法来从具有属性的NSObject转换为NSDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我最终想出的东西,但认为可能有一种更有效的方法来实现它.

I ran across something that I eventually figured out, but think that there's probably a much more efficient way to accomplish it.

我有一个对象(一个采用MKAnnotation协议的NSObject),它具有许多属性(标题,字幕,纬度,经度,信息等).我需要能够将此对象作为NSDictionary传递给另一个对象,该对象想要使用objectForKey方法从该对象中提取信息(因为这就是从另一个视图控制器获取的内容).

I had an object (an NSObject which adopted the MKAnnotation protocol) that had a number of properties (title, subtitle,latitude,longitude, info, etc.). I needed to be able to pass this object to another object, which wanted to extract info from it using objectForKey methods, as an NSDictionary (because that's what it was getting from another view controller).

我最终要做的是创建一个新的NSMutableDictionary,并在其上使用setObject:forKey传输每条重要信息,然后我就传递了新创建的字典.

What I ended up doing was create a new NSMutableDictionary and use setObject: forKey on it to transfer each piece of vital info, and then I just passed on the newly created dictionary.

是否有更容易的方法来实现此目的?

Was there an easier way to do this?

以下是相关代码:

// 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;
    }
}

预先感谢您的集体智慧.

Thanks in advance for your collective wisdom.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{    
if ([[segue identifier] isEqualToString:@"showDetailFromMap"]) 
{
    DetailViewController *dest =[segue destinationViewController];
    NSArray *myKeys = [NSArray arrayWithObjects:
@"title",@"subtitle",@"info",@"pic",@"latitude",@"longitude", nil];

    //make a dictionary from annotaion to pass info
    NSDictionary *myValues =[sender dictionaryWithValuesForKeys:myKeys];

    //pass values
    dest.curLoc = myValues;
}

}

还有一个更简单的修复程序,如下所示...

使用valueForKey而不是对象作为密钥来检索信息.

}

And a even simpler fix, as seen below...

Using valueForKey instead of object for key to retrieve the information.

推荐答案

如果属性的名称与用于访问字典的键的名称相同,则您可以使用KVC并使用valueForKey:而不是objectForKey.

If the properties had the same names as the keys used to access the dictionary then you could have just used KVC and had valueForKey: instead of objectForKey.

例如,给出此词典

NSDictionary *annotation = [[NSDictionary alloc] initWithObjectsAndKeys:
                             @"A title", @"title", nil];

和该对象

@interface MyAnnotation : NSObject

@property (nonatomic, copy) NSString *title;

@end

是否拥有字典实例或MyAnnotation我可以致电

it wouldn't matter if I had an instance of the dictionary or MyAnnotation I could call

[annotation valueForKey:@"title"];


显然,这也可以通过其他方式实现,例如


Obviously that works the other way as well e.g.

[annotation setValue:@"A title" forKey:@"title"];

这篇关于OBJ-C简单的方法来从具有属性的NSObject转换为NSDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆