我收到以下错误:
“此类不是密钥值编码兼容的密钥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数据答案 0 :(得分:3)
CurrentWeather具有degreesF
属性,您可以从temp_f
XML字段设置该属性。您需要将表列的标识符设置为“degreesF”而不是“temp_f”。这与包含下划线的temp_f
无关。相反,问题是CurrentWeather不是键值“temp_f”的键值编码(就像错误状态一样),因为它没有名为“temp_f”的属性。
进一步详细解释,在-tableView:objectValueForTableColumn:
方法中,使用列的标识符作为CurrentWeather实例的键。由于标识符是“temp_f”,因此您执行此操作:[c valueForKey:@"temp_f"]
。因为CurrentWeather没有temp_f
属性,所以会引发异常。