无法插入NSMutableDictionary

时间:2017-06-10 09:02:10

标签: ios objective-c nsmutabledictionary

这是我初始化它的方式:

@property (strong, nonatomic) NSMutableDictionary<NSString *, UIImage *> *images;

这是我插入的方式:

NSString *stringDate = [self getddMMyyyyInString:firstDate]

[[self.images setObject:[UIImage imageNamed:@"rsz_fill.png"] forKey:stringDate]];

我得到的错误是

  

索引表达式无效,因为下标类型void不是   积分或目标c指针类型

如何将NSStringUIImage插入NSmutabledictionary

2 个答案:

答案 0 :(得分:2)

@property (strong, nonatomic) NSMutableDictionary<NSString *, UIImage *> *images;


self.images = [NSMutableDictionary new];

NSString *stringDate = [NSString stringWithFormat:@"%@", [NSDate date]];

UIImage *image = [UIImage imageNamed:@"rsz_fill.png"];

[self.images setObject:image forKey:stringDate];

答案 1 :(得分:0)

当期望基本类型但提供指针时会发生此问题。

下面的代码对我来说很好。试试吧。

@property (strong, nonatomic) NSMutableDictionary<NSString *, UIImage *> *images;

//inside some method (say viewDidLoad)

NSString *stringDate = [NSString stringWithFormat:@"%@", [NSDate date]];

[self.images setObject:[UIImage imageNamed:@"someImage.png"] forKey:stringDate];

另请参阅此link