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

烈酒焚心 提交于 2020-01-07 02:35:08

问题


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

I am having trouble with this line:

NSDateFormatter *cachedDateFormatter = formatters[hashKey];

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

Some context:

    + (NSMutableDictionary<NSString *, NSDateFormatter *>*)sharedDateFormatters {
    static NSMutableDictionary *dict = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dict = [NSMutableDictionary new];
    });
    return dict;
    }

回答1:


What is the return type of your sharedDateFormatters method?

What data type did you declare the formatters variable?

See the disconnect? They need to match. You can't assign an NSDictionary value to a variable of type NSDateFormatter.

Change your line to:

NSDictionary *formatters = [NSDate sharedDateFormatters];

or:

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


来源:https://stackoverflow.com/questions/36653654/expected-method-to-read-dictionary-element-not-found-on-object-of-type-nsdatefo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!