NSDateComponents - EXC_BAD_ACCESS错误 [英] NSDateComponents - EXC_BAD_ACCESS error
问题描述
我相信有一个简单的答案的基本问题。
Basic question to which I'm sure there is a simple answer.
我试图获取照片的时间戳。当我试图访问NSDateComponents检索一个特定的日期元素(例如天),我得到一个EXC_BAD_ACCESS错误。
I'm trying to get the timestamp of a photo. When I try to access the NSDateComponents to retrieve a specific date element (say "day" for example), I get a EXC_BAD_ACCESS error.
首先,我的相关位代码:
First, the relevant 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"
如果我注释掉最后一行: em> NSLog(@comp:%@,days); 代码执行成功。
If I comment out the final line: NSLog(@"comp: %@", days); the code executes successfully.
底线是,任何时候我发送消息到组件以访问属性时产生了EXC_BAD_ACCESS错误。
I've tried a few variants on this but the bottom line is that any time I send a message to components to access a property the "EXC_BAD_ACCESS" error is raised.
似乎表明这通常是未初始化的指针或我试图发布的对象的结果,这不是我的发布。
The research I did seems to indicate that this is typically the result of an uninitialized pointer or an object that I've tried to release that isn't mine to release. From what I can tell that's not the case here.
我错过了什么?
这对我来说有些光。这是我在Objective-C中的第一次探索。
Thanks in advance for shedding some light on this for me. This is one of my first explorations in Objective-C.
推荐答案
您使用%@
作为整数的格式说明符。您需要使用%d
。
You are using %@
as the format-specifier for an integer. You need to use %d
.
当您使用%@
, NSLog
函数将该参数视为一个对象,并发送 description
消息。真正发生的是,运行时试图去引用参数(整数),但因为它不是一个有效的指针,你会得到一个 EXC_BAD_ACCESS
异常。
When you use %@
, the NSLog
function will treat the argument as an object, and send it the description
message. What really happens, is that the runtime tries to dereference the argument (the integer) but because it is not a valid pointer, you get an EXC_BAD_ACCESS
exception.
这篇关于NSDateComponents - EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!