Converting NSDate to NSString causes unrecognized selector exception

前端 未结 2 984
情歌与酒
情歌与酒 2021-01-25 18:30

I am storing an NSDate in a plist as a string, and at launch I am trying to convert the string from the plist back to an NSDate to compare it later.

相关标签:
2条回答
  • 2021-01-25 19:08

    You are overcomplicating things. What makes you think that storing a NSDate object you'll get back a NSString?

    Just do

    NSDate * checkDate = [InfoDic objectForKey:@"LastDate"];
    

    Also, don't confuse KVC methods with NSDictionary methods.

    You want to use setObject:forKey: instead of setValue:forKey if you don't want to face bad surprises.

    0 讨论(0)
  • 2021-01-25 19:21

    You are not storing a date as a string in the plist, you are storing it as a date.

    The line:

    [InfoDic setValue:[NSDate date] forKey:@"LastDate"];
    

    stores the actual NSDate object.

    All you need to get it back out is to call:

    NSDate *theDay = InfoDic[@"LastDate"];
    

    BTW - the line:

    [InfoDic setValue:[NSDate date] forKey:@"LastDate"];
    

    should be:

    [InfoDic setObject:[NSDate date] forKey:@"LastDate"];
    

    or just:

    InfoDic[@"LastDate"] = [NSDate date];
    
    0 讨论(0)
提交回复
热议问题