开发者

iPhone PList and NSDate issues

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 inde开发者_StackOverflowx = [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.


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.

NSLog(@"%@", [[alarm datetime] class]);

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜