将NSDate转换为NSString会导致无法识别的选择器异常 [英] Converting NSDate to NSString causes unrecognized selector exception

查看:279
本文介绍了将NSDate转换为NSString会导致无法识别的选择器异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个 NSDate 作为字符串存储在plist中,在启动时,我试图将plist的字符串转换回

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.

这是我如何将值存储在我的plist:

This is how I am storing the value in my plist:

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

在日志中(当我转换 [NSDate date] 到一个正确的字符串)它说:

In the log (When I convert the [NSDate date] to a proper string) it says this:

2013-04-13 22:47:57 +0000

这是我试图将plist值转换回 NSDate

This is how I am trying to convert the plist value back to an NSDate:

NSString *Checkdate= [InfoDic objectForKey:@"LastDate"];
NSDateFormatter *DateFormat=[[NSDateFormatter alloc]init];
[DateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *theday=[DateFormat dateFromString:Checkdate];

这是我的iPhone 4S上的错误日志:

This is the error log on my iPhone 4S:

<Error>: -[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0'
    *** First throw call stack:
(0x31e5c2a3 0x39b7997f 0x31e5fe07 0x31e5e531 0x31db5f68 0x327c213d 0x327c208d 0x327c237b 0x23fe1 0x33c83595 0x33cc3d79 0x33cbfaed 0x33d011e9 0x2385d 0x33cc4ad9 0x33cc4663 0x33cbc84b 0x33c64c39 0x33c646cd 0x33c6411b 0x3597a5a3 0x3597a1d3 0x31e31173 0x31e31117 0x31e2ff99
0x31da2ebd 0x31da2d49 0x33cbb485 0x33cb8301 0x235a1 0x23528)

请注意,当我在我的设备上安装应用程序时,它需要第一个日期并将其存储在plist中。

Please note that when I install the app on my device it takes the first date and stores it in the plist. When I close the app and rerun it, it gives me the SIGABRT.

我应该怎么办?

推荐答案

你过于复杂的事情。什么让你认为存储一个 NSDate 对象你会得到一个 NSString

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

只是做

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

此外,不要混淆 KVC 方法与的NSDictionary 方法

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

您想要使用的setObject:forKey:而不是 setValue:forKey ,如果你不想面对不好的惊喜。

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

这篇关于将NSDate转换为NSString会导致无法识别的选择器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆