NSDateComponents Error - EXC_BAD_ACCESS

The main question I'm sure is the simple answer.

I am trying to get the timestamp of a photo. When I try to access NSDateComponents to get a specific date element (for example, "day"), I get an EXC_BAD_ACCESS error.

First, the corresponding bits of my code:

// formattedDateString is a string representing the "DateTimeOriginal" EXIF property extracted from the image   
NSDate *takenAt = [[NSDate alloc] initWithString:formattedDateString];
NSLog(@"date: %@", takenAt); // prints= date: 2010-01-10 03:25:00 -0500

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSLog(@"cal: %@", gregorian); // prints= cal: <__NSCFCalendar: 0x10020b570>

NSUInteger unitFlags =  (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit);      
NSDateComponents *components = [gregorian components: unitFlags 
                                            fromDate: takenAt
                               ];

int days = [components hour];
NSLog(@"comp: %@", days); // thrown error= Program received signal:  "EXC_BAD_ACCESS"

If I comment on the final line: NSLog (@ "comp:% @", days); The code runs successfully.

I tried several options, but the bottom line says that at any time when I send a message to the components to access the property, the error "EXC_BAD_ACCESS" occurs.

, , , , , , , , . , , .

?

, . Objective-C.

NSDateComponents.

+3
1

%@ . %d.

%@, NSLog description. , , ( ), , EXC_BAD_ACCESS.

+7

Source: https://habr.com/ru/post/1731960/


All Articles