作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试按照指令将 NSString 转换为 NSDate:NSString to NSdate
我的代码片段不起作用,可能源 NSString 提取不正确 来自 ABPerson,带有 kABBirthdayProperty。它得到错误的原因:
-[__NSTaggedDate bytes]: unrecognized selector sent to instance 0xc1c3a7619800000d
这是片段:
-(void) abRead: (ABPerson*) myRecord
{ if ([myRecord valueForProperty:kABBirthdayProperty] != nil)
{ NSDateFormatter* myBirthday = [[NSDateFormatter alloc] init];
[myBirthday setDateFormat:@"YYYY-DD-MM HH:MM:SS"];
NSDate* birthDay = [myBirthday dateFromString:[myRecord valueForProperty:kABBirthdayProperty]];
}
我猜想从 kABBirthdayProperty 得到的 NSString 有一些特殊的格式或其他东西。我查了一下:
NSLog(@"birthday: %@", [myRecord valueForProperty:kABBirthdayProperty]);
它打印出来的 NSString 应该是:
birthday :1980-02-08 05:00:00 +0000
我是否需要从 ABPerson:kABProperty 到 NSString 的正确转换?
抱歉我的英语不好。
最佳答案
该属性已经是一个 NSDate*
,它不是一个 NSString*
。因此,您不应尝试将其从 NSString*
转换为 NSDate*
并直接将其用作 NSDate*
:
-(void) abRead: (ABPerson*) myRecord {
NSDate* birthDay = [myRecord valueForProperty:kABBirthdayProperty];
if (birthday != nil) {
// Do something with birthday
}
}
您得到的错误实际上告诉您:
-[__NSTaggedDate bytes]: unrecognized selector sent to instance 0xc1c3a7619800000d
这告诉您 __NSTaggedDate
类型的实例正在调用 bytes
方法。这意味着您将一种类型视为另一种类型。
关于ios - 无法将 NSString 从 kABBirthdayProperty 转换为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33763862/
我尝试按照指令将 NSString 转换为 NSDate:NSString to NSdate 我的代码片段不起作用,可能源 NSString 提取不正确 来自 ABPerson,带有 kABBirt
我是一名优秀的程序员,十分优秀!