我有这样的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<background>assets/_image.png</background>
<items>
<item type='test1' position='6' x='123' y='456'>
my_way
</item>
<item type='test2' position='8' x='456' y='123'>
another_way
</item>
.........................
我使用NSXMLParser读取了它:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([self.currentTag isEqualToString:@"item"]) {
[self addItemForAttributes:self.currentAttributes withValue:string];
NSLog(@"%@", self.items.lastObject.type);
NSLog(@"%@", self.items.lastObject.position);
NSLog(@"%@", NSStringFromCGPoint(self.items.lastObject.coordinate));
NSLog(@"%@", self.items.lastObject.value);
}
}
我将项目写入项目数组。但是当我在控制台中阅读它时,有些项目看起来像是重复的。
2019-07-02 17:25:53.326939+0300 Game[1280:235820] test1
2019-07-02 17:25:53.327083+0300 Game[1280:235820] 6
2019-07-02 17:25:53.327445+0300 Game[1280:235820] {123, 456}
2019-07-02 17:25:53.327671+0300 Game[1280:235820] my_way
2019-07-02 17:25:53.327946+0300 Game[1280:235820] test1
2019-07-02 17:25:53.328021+0300 Game[1280:235820] 6
2019-07-02 17:25:53.328301+0300 Game[1280:235820] {123, 456}
2019-07-02 17:25:53.328348+0300 Game[1280:235820]
2019-07-02 17:25:53.328991+0300 Game[1280:235820] test1
2019-07-02 17:25:53.329112+0300 Game[1280:235820] 6
如何解决?
答案 0 :(得分:0)
多次调用委托方法(找到字符),我为此做条件(过滤空字符串),所以解决了我的问题。