开发者

objective c NSInvalidArgumentException only in device - not in simulator

I'm using NSXMLParser to parse a document and display the contents in a UITableView.

Each element i extract from the XML is added into an NSMutableArray, and then the cells of the UITableView are populated using this NSMutableArray from the cellForRowAtIndexPath method.

(So, my interface looks like this:

@interface ncViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate>

)

Here is how i add each element into the NSMutableArray:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if ([elementName isEqualToString:@"item"]){

        [item setObject:currentTitle forKey:@"title"];  // item is an NSMutableDictionary
        [item setObject:currentLink forKey:@"link"];
        [item setObject:[self flattenHTML:currentSummary] forKey:@"summary"];

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
        NSDate *qdate = [dateFor开发者_Python百科mat dateFromString:[currentDate stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
        [item setObject:qdate forKey:@"date"];

        [dateFormat release];
        dateFormat = nil;

        [stories addObject:[[item copy] autorelease]];


    }
}

When i test the app in Simulator everything works fine, but when i test it on a device (iPhone), the app crashes due to the following "uncaught exception":

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
 '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: date)'

Why is qdate nil? what changes when running the app in device instead of simulator?

Any suggestions? I hope somebody has an idea, as it's a bit urgent...

Thanks in advance!


If the device's Region setting doesn't match the language the date strings are in, the formatter will not be able to parse the string.

If the strings are in English, you can set the locale on the formatter so it matches the string's language:

NSLocale *enUSPOSIXLocale = 
    [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
[dateFormat setLocale:enUSPOSIXLocale];
NSDate *qdate = [dateFormat dateFromString...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜