我正在尝试创建一个从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对象中分配数组?
WeatherData.h
@interface WeatherData : NSObject
@property (strong, nonatomic)NSString *town;
@property (strong, nonatomic)NSString *country;
@property (strong, nonatomic)NSMutableArray *timedata;
@end
WeatherData.m
@implementation WeatherData
@synthesize town = _town;
@synthesize country = _country;
@synthesize timedata = _timedata;
@end
WeatherTimeData.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
WeatherTimeData.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
答案 0 :(得分:1)
这种类型的问题是典型的,因为尚未分配对象,并且给定Objective-C允许将消息发送到程序员不注意的nil
。
我认为能够提供运行时环境变量并将此类事件记录到stdout中会很好...
我怀疑[WeatherTimeData init]
没有创建timedata
,那是因为你没有为它提供实现;仅使用@sythensize
不足以创建对象。