1

I tried to convert NSString to NSDate as the instruction: NSString to NSdate

My snippet does not work, maybe the source NSString extracts incorrect from ABPerson, with kABBirthdayProperty. It gets error with reason:

-[__NSTaggedDate bytes]: unrecognized selector sent to instance 0xc1c3a7619800000d

This is the snippet:

-(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]];
   }

I guess that the NSString got from kABBirthdayProperty have some special format or some thing. I checked:

NSLog(@"birthday: %@", [myRecord valueForProperty:kABBirthdayProperty]);

it print out as NSString should be:

birthday :1980-02-08 05:00:00 +0000

Do I need any conversion from ABPerson:kABProperty to NSString correctly?

sorry for my bad English.

Community
  • 1
  • 1
ML1426
  • 23
  • 5

1 Answers1

1

That property is already an NSDate*, it is not an NSString*. As such, you should not try and convert it from an NSString* to an NSDate* and just use it as an NSDate* directly:

-(void) abRead: (ABPerson*) myRecord {
    NSDate* birthDay = [myRecord valueForProperty:kABBirthdayProperty];
    if (birthday != nil) {
        // Do something with birthday
    }
}

The error you are getting actually tells you this:

-[__NSTaggedDate bytes]: unrecognized selector sent to instance 0xc1c3a7619800000d

This is telling you that an instance of type __NSTaggedDate is having the method bytes called on it. This implies that you're treating a type as if it is a different type.

Charles A.
  • 10,685
  • 1
  • 42
  • 39