我正在尝试制作一个从 YR.no-API 保存数据的 XML 解析器。解析器应该将数据添加到这两个数据结构中。我的问题是当我尝试将 WeatherTimeData 对象添加到 WeatherData 中的数组时,似乎没有添加对象,以下计数总是给我零
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:@"time"])
{
[self.weatherdata.timedata addObject:currentWeatherTimeData];
NSLog(@"amount of objects in timedata-array %d", [[self.weatherdata timedata] count]);
}
这是我的两个数据结构,我在以下方法中分配 weatherData
-(id) loadXMLByURL:(NSString *)urlString
{
_weatherdata = [[WeatherData alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
我必须在 weatherdata 对象内分配数组吗?
天气数据.h
@interface WeatherData : NSObject
@property (strong, nonatomic)NSString *town;
@property (strong, nonatomic)NSString *country;
@property (strong, nonatomic)NSMutableArray *timedata;
@end
天气数据.m
@implementation WeatherData
@synthesize town = _town;
@synthesize country = _country;
@synthesize timedata = _timedata;
@end
天气时间数据.h
@interface WeatherTimeData : NSObject
@property (strong, nonatomic)NSDate *startTime;
@property (strong, nonatomic)NSDate *endTime;
@property (strong, nonatomic)NSString *iconSymbol;
@property (strong, nonatomic)NSString *windDirection;
@property (strong, nonatomic)NSString *windSpeed;
@property (strong, nonatomic)NSString *temprature;
@property (strong, nonatomic)NSString *preassure;
@end
天气时间数据.m
@implementation WeatherTimeData
@synthesize startTime = _startTime;
@synthesize endTime = _endTime;
@synthesize iconSymbol = _iconSymbol;
@synthesize windDirection = _windDirection;
@synthesize windSpeed = _windSPeed;
@synthesize temprature = _temprature;
@synthesize preassure = _preassure;
@end