【问题标题】:How to init a NSMutableDictionary object with numbers and UIImages如何使用数字和 UIImages 初始化 NSMutableDictionary 对象
【发布时间】:2015-05-13 15:59:38
【问题描述】:

我知道在 Python 中,我可以像这样初始化一个字典:

numbers = range(1,5)
objs = [pic1, pic2, pic3, pic4]
dict_obj = dict(zip(numbers, objs))

pic1-pic4 是类的实例等对象。

我的问题是我可以在 Objective-C 中初始化一个 NSMutableDictionary 对象,就像上面的代码一样,在 NSArray 和包含一些 UIImage 元素的 NSMutableArray 中使用数字,我不知道 NSMutableDictionary 中的键是否可以是指针或对象,但不能只有数字还是字符串?

【问题讨论】:

  • 您的问题真的是NSMutableDictionary 可接受的密钥是什么”

标签: python objective-c


【解决方案1】:

是的,可以。 Dictionary 定义的模板本身表明它接受包含对象的数组作为设置对象和键的参数

NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjects:(NSArray *) forKeys:(NSArray *)];

你的答案应该是这样的。

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:@[@1,@2,@3,@4] forKeys:@[pics1,pics2,pics3,pics4]];

【讨论】:

    【解决方案2】:

    根据文档,

    Each key is copied (using copyWithZone:; keys must conform to the NSCopying protocol), 
    and the copy is added to the new dictionary.
    

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 2014-05-18
      • 2020-04-21
      • 2016-03-11
      相关资源
      最近更新 更多