我在objective-c 中创建了一个XMLParser 类,我似乎无法在解析器进程中的共享存储中设置iVar,我尝试了很多方法,但我无处可去。
这是我的代码和返回的内容,希望这是我忽略的一个小语法错误。
。H
@interface XMLParser : NSXMLParser <NSXMLParserDelegate>
{
XMLParser *XMLStore;
}
@property(nonatomic, weak)NSMutableString *longitudeValue;
@property(nonatomic, weak)NSMutableString *latitudeValue;
+ (XMLParser *)sharedStore;
- (void)parseXMLAtURL:(NSURL *)url;
@end
.m
#import "XMLParser.h"
@implementation XMLParser
BOOL blockLatLong = NO;
NSMutableString *currentNodeContent;
+ (XMLParser *)sharedStore
{
static XMLParser *XMLStore = nil;
if (!XMLStore)
XMLStore = [[XMLParser alloc] init];
return XMLStore;
}
- (void)parseXMLAtURL:(NSURL *)url
{
NSXMLParser *parser = [[XMLParser alloc] initWithContentsOfURL:url];
parser.delegate = self;
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSLog(@"Long:%@, Lat:%@", XMLStore.longitudeValue, XMLStore.latitudeValue);
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = [NSMutableString stringWithString:string];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"geometry"]){
blockLatLong = YES;
}
if ([elementName isEqualToString:@"location_type"]){
blockLatLong = NO;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (blockLatLong){
if ([elementName isEqualToString:@"lng"]){
[XMLStore setLongitudeValue:currentNodeContent];
NSLog(@"%@", currentNodeContent);
NSLog(@"%@", XMLStore.longitudeValue);
}
if ([elementName isEqualToString:@"lat"]){
[XMLStore setLatitudeValue:currentNodeContent];
NSLog(@"%@", currentNodeContent);
NSLog(@"%@", XMLStore.latitudeValue);
}
}
}
@end
日志
2013-09-23 11:19:59.606 Weathalert[640:c07] 40.7143528
2013-09-23 11:19:59.606 Weathalert[640:c07] (null)
2013-09-23 11:19:59.607 Weathalert[640:c07] -74.0059731
2013-09-23 11:19:59.607 Weathalert[640:c07] (null)
2013-09-23 11:19:59.607 Weathalert[640:c07] Long:(null), Lat:(null)