Я получаю следующую ошибку:
"этот класс не соответствует значению ключа для кода temp_f"
мои файлы класса AppDelegate:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTableView *tableView;
NSArray *current;
}
@property (assign) IBOutlet NSWindow *window;
@end
#import "AppDelegate.h"
#import "CurrentWeather.h"
#import "XMLCurrent.h"
@implementation AppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
XMLCurrent *currentXML = [[XMLCurrent alloc] init];
NSError *error = nil;
current = [currentXML fetchCurrentWithError:&error];
[tableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)theTableView {
return [current count];
}
- (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
CurrentWeather *c = [current objectAtIndex:row];
return [c valueForKey:[tableColumn identifier]];
}
@end
мои файлы класса CurrentWeather:
#import <Foundation/Foundation.h>
@interface CurrentWeather : NSObject {
NSString *location;
NSString *weather;
NSString *degreesF;
}
@property (nonatomic, copy) NSString *location;
@property (nonatomic, copy) NSString *weather;
@property (nonatomic, copy) NSString *degreesF;
@end
#import "CurrentWeather.h"
@implementation CurrentWeather
@synthesize location, weather, degreesF;
@end
мои файлы класса XMLCurrent:
#import <Foundation/Foundation.h>
@interface XMLCurrent : NSObject <NSXMLParserDelegate> {
NSMutableArray *current;
NSMutableString *currentString;
NSMutableDictionary *currentFields;
}
- (NSArray *)fetchCurrentWithError:(NSError **)outError;
@end
#import "XMLCurrent.h"
#import "CurrentWeather.h"
@implementation XMLCurrent
- (id)init {
self = [super init];
if (self) {
current = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)fetchCurrentWithError:(NSError **)outError {
BOOL success;
NSURL *xmlURL = [NSURL URLWithString:@"http://www.weather.gov/xml/current_obs/KCLT.xml"];
NSURLRequest *req = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSURLResponse *resp = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:outError];
if (!data) {
return nil;
}
[current removeAllObjects];
NSXMLParser *parser;
parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
success = [parser parse];
if (!success) {
*outError = [parser parserError];
return nil;
}
NSArray *output = [current copy];
return output;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqual:@"current_observation"]) {
currentFields = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqual:@"current_observation"]) {
CurrentWeather *currentCond = [[CurrentWeather alloc] init];
[currentCond setLocation:[currentFields objectForKey:@"location"]];
[currentCond setWeather:[currentFields objectForKey:@"weather"]];
[currentCond setDegreesF:[currentFields objectForKey:@"temp_f"]];
[current addObject:currentCond];
currentCond = nil;
currentFields = nil;
} else if (currentFields && currentString) {
NSString *trimmed;
trimmed = [currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[currentFields setObject:trimmed forKey:elementName];
}
currentString = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentString) {
currentString = [[NSMutableString alloc] init];
}
[currentString appendString:string];
}
@end
Ключи используются в качестве «идентификатора» в табличном представлении.По какой-то причине, если в ключе есть подчеркивание (например, temp_f), я получаю сообщение об ошибке.Подчеркивание необходимо, потому что это имя элемента в файле XML.Если нет подчеркивания, то нет ошибки.Как я могу получить данные из элемента XML, который содержит подчеркивание?
Данные xml анализируются с http://www.weather.gov/xml/current_obs/KCLT.xml