NSString *hashKey = [NSString stringWithFormat:@"%lu%lu%lu", (unsigned long)format.hash, (unsigned long)timeZone.hash, (unsigned long)locale.hash];
NSDateFormatter *formatters = [NSDate sharedDateFormatters];
NSDateFormatter *cachedDateFormatter = formatters[hashKey];
我遇到这条线路有问题:
NSDateFormatter *cachedDateFormatter = formatters[hashKey];
告诉我:在“NSDateFormatter *”类型的对象上找不到字典元素的预期方法
某些背景信息:
+ (NSMutableDictionary<NSString *, NSDateFormatter *>*)sharedDateFormatters {
static NSMutableDictionary *dict = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dict = [NSMutableDictionary new];
});
return dict;
}
答案 0 :(得分:1)
sharedDateFormatters
方法的返回类型是什么?
您声明了formatters
变量的数据类型是什么?
看到断开连接?他们需要匹配。您无法为NSDictionary
类型的变量分配NSDateFormatter
值。
将您的行更改为:
NSDictionary *formatters = [NSDate sharedDateFormatters];
或:
NSDictionary<NSString *, NSDateFormatter *> *formatters = [NSDate sharedDateFormatters];