繁体   English   中英

无法将NSString从kABBirthdayProperty转换为NSDate

[英]Failed in convert NSString from kABBirthdayProperty to NSDate

我尝试将NSString转换为NSDate作为指令: NSString转换为NSdate

我的代码段不起作用,可能是源NSString使用kABBirthdayProperty从ABPerson提取了不正确的代码。 它因原因而出错:

-[__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字节]:无法识别的选择器已发送到实例0xc1c3a7619800000d

这是在告诉您__NSTaggedDate类型的实例正在调用方法bytes 这意味着您将一种类型视为另一种类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM