gpt4 book ai didi

ios - 无法将 NSString 从 kABBirthdayProperty 转换为 NSDate

转载 作者:行者123 更新时间:2023-11-29 12:09:56 27 4
gpt4 key购买 nike

我尝试按照指令将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com