无法插入 NSMutableDictionary

Unable to insert into NSMutableDictionary

我是这样初始化的:

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

这是我插入的方式:

NSString *stringDate = [self getddMMyyyyInString:firstDate]

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

我得到的错误是

indexing expression is invalid because subscript type void is not an integral or objective c pointer type

如何将 NSStringUIImage 插入 NSmutabledictionary

当需要基本类型但提供了指针时会出现此问题。

下面的代码对我来说工作正常。试试吧。

@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

@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];