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

时间:2016-04-15 17:43:24

标签: objective-c 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;
    }

1 个答案:

答案 0 :(得分:1)

sharedDateFormatters方法的返回类型是什么?

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

看到断开连接?他们需要匹配。您无法为NSDictionary类型的变量分配NSDateFormatter值。

将您的行更改为:

NSDictionary *formatters = [NSDate sharedDateFormatters];

或:

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