读取字典元素的预期方法在 "NSDateFormatter*" 类型的对象上找不到

expected method to read dictionary element not found on object of type "NSDateFormatter*"

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

您的 sharedDateFormatters 方法的 return 类型是什么?

您声明的 formatters 变量是什么数据类型?

看到断开连接了吗?他们需要匹配。您不能将 NSDictionary 值分配给 NSDateFormatter.

类型的变量

将您的线路更改为:

NSDictionary *formatters = [NSDate sharedDateFormatters];

或:

NSDictionary<NSString *, NSDateFormatter *> *formatters = [NSDate sharedDateFormatters];