0

I've such xml-file:

<?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>

.........................

I read it using 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);
    }
}

I write item into items array. But when I read it in console, some items looks as duplicated.

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

How to fix it?

pragmus
  • 3,513
  • 3
  • 24
  • 46
  • There's far too much missing information in your question. The code you posted has many variables you have not told us anything about. Also note that `foundCharacters` can be called many times. Normally you initialize a mutable string when an element starts. You append `string` to the mutable string in each call to `foundCharacters`, and you then look at the result when the element ends. – rmaddy Jul 02 '19 at 15:48
  • @maddy, yes, I just noticed it. – pragmus Jul 02 '19 at 16:09

1 Answers1

0

Delegate method(found characters) calls several times, I made condition for it(filter empty strings), so it solved my problem.

pragmus
  • 3,513
  • 3
  • 24
  • 46
  • That's not a good solution. For longer strings, `foundCharacters` can be called multiple times, each with a piece of the longer string. Don't assume each call to `foundCharacters` contains a complete value. – rmaddy Jul 02 '19 at 16:15