iPhone PList 和 NSDate 问题
我正在做:
NSString *path = [[self class] pathForDocumentWithName:@"Alarms.plist"];
NSArray *alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
if (alarmDicts == nil)
{
NSLog(@"MER. Unable to read plist file: %@", path);
path = [[NSBundle mainBundle] pathForResource:@"Alarms"
ofType:@"plist"];
alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
}
_displayedObjects = [[NSMutableArray alloc]
initWithCapacity:[alarmDicts count]];
for (NSDictionary *currDict in alarmDicts)
{
Alarm *alarm = [[Alarm alloc] initWithDictionary:currDict];
[_displayedObjects addObject:alarm];
}
pathForDocumentWithName 只是一个辅助方法,假设它有效(它确实有效)。我将 plist 的所有值添加到一个对象中,并将其存储在一个数组中。现在,如果我这样做:
NSUInteger index = [indexPath row];
id alarm = [[self displayedObjects] objectAtIndex:index];
NSString *title = [alarm title];
[[cell detailTextLabel] setText:title];
它工作得很好。但是,当尝试在 plist 文件中格式化 NSDate 类型(列为“datetime”)时,
NSDate *datetime = [alarm datetime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm"];
[[cell textLabel] setText:[formatter stringFromDate:datetime]];
它会抛出 AlarmDicts 为 nil 的 NSLog,并为字符串返回 nil。我没有主意,并且已经尝试了几个小时来解决这个问题。有人有什么想法吗?
另外,如果我打印出日期时间的描述,它就可以完美工作。当我尝试在其上使用 NSDateFormatter 时,只有 nils 和错误。
I am doing:
NSString *path = [[self class] pathForDocumentWithName:@"Alarms.plist"];
NSArray *alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
if (alarmDicts == nil)
{
NSLog(@"MER. Unable to read plist file: %@", path);
path = [[NSBundle mainBundle] pathForResource:@"Alarms"
ofType:@"plist"];
alarmDicts = [NSMutableArray arrayWithContentsOfFile:path];
}
_displayedObjects = [[NSMutableArray alloc]
initWithCapacity:[alarmDicts count]];
for (NSDictionary *currDict in alarmDicts)
{
Alarm *alarm = [[Alarm alloc] initWithDictionary:currDict];
[_displayedObjects addObject:alarm];
}
pathForDocumentWithName just a helper method, assume it works (it does). I add all of the values of the plist to an object, and store it in an array. Now if I do something like this:
NSUInteger index = [indexPath row];
id alarm = [[self displayedObjects] objectAtIndex:index];
NSString *title = [alarm title];
[[cell detailTextLabel] setText:title];
It works perfectly fine. But when trying to format the NSDate type in the plist file (listed as 'datetime')
NSDate *datetime = [alarm datetime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm"];
[[cell textLabel] setText:[formatter stringFromDate:datetime]];
It throws the NSLog for alarmDicts being nil, and returns nil for the string. I'm out of ideas, and have been trying for a few hours to solve this. Anyone have any ideas?
Also, if I print out the description for datetime, it works perfectly. Only nils and errors out when I attempt to use the NSDateFormatter on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很大的猜测,但是您确定属性列表中的日期被作为 NSDate 对象读入吗?如果我是你,我会检查你明显的 NSDate 对象的类型,例如,
我会怀疑它们是否被加载为 NSStrings,NSDateFormatter 将拒绝处理它们 - 但它们仍然会正确记录。
不相关的评论:我确定这只是复制和粘贴错误,但您在第一个代码片段的底部泄漏了“警报”对象。
A massive guess, but are you sure the dates in your property list are being read in as NSDate objects? If I were you, I'd check the type of your apparent NSDate objects, e.g.
I would be suspicious that they're being loaded as NSStrings, which NSDateFormatter will decline to process — but they'll still appear to log correctly.
Unrelated comment: I'm sure it's a copy and paste error only, but you're leaking 'Alarm' objects at the bottom of your first snippet of code.