Objective-c - XMLReader no funciona correctamente

I have a problem with the class XMLReader ( https://github.com/Insert-Witty-Name/XML-to-NSDictionary ).

When i parse this document, it's work perfectly

<webservices>
    <title>Photos</title>
    <item>
       <name>test</name>
    </item> 
    <item>
       <name>test2</name>
    </item> 
</webservices>

The programme display :

  webservices = {
        item = (
                  {
                    name= "test1.";
                  },
                  {
                     name= "test2";
                  }
              );
        title = Photos;
    };
}

But when i have one Item, it's doesn't work. I'm not a NSarray of Item but a NSDictionnary with directy the node "name".

<webservices>
    <title>Photos</title>
    <item>
       <name>test</name>
    </item> 
</webservices>

The programme display :

  webservices = {
        item =    {
                     name= "test";
                  }
        title = Photos;
    };
}

A idea ?

Muchas gracias :)

preguntado el 09 de marzo de 12 a las 14:03

2 Respuestas

prueba este

XMLReader.m

+(NSArray*)enforceArray:(id)payload{
    if ([payload isKindOfClass:[NSArray class]]){
        return payload;
    }else{
        if ([payload isKindOfClass:[NSNull class]] || isEmpty(payload)) return [NSArray array];
        return [NSArray arrayWithObject:payload];
    }
}



//common.h
static inline BOOL isEmpty(id thing) {
    return thing == nil
    || [thing isKindOfClass:[NSNull class]]
    || [thing respondsToSelector:@selector(isEqualToString)]
        && [(NSString*)thing isEqualToString:@"(null)"]
    || ([thing respondsToSelector:@selector(length)]
        && [(NSData *)thing length] == 0)
    || ([thing respondsToSelector:@selector(count)]
        && [(NSArray *)thing count] == 0);
}


 NSMutableArray *results  = [[NSMutableArray arrayWithArray:[XMLReader enforceArray:[[xmlDictionary objectForKey:@"notifications"] objectForKey:@"notification"]]];

one day I'll dig through the core class to fix this at the root of problem. here's a bandaid in the mean time.

Respondido 23 ago 12, 11:08

That's normal - if there is only one item, there's no need to wrap a single item in an array. Your handler just needs to get the [webservice valueForKey:@"item"] como un id, then check what class it is. If it's not an NSArray, just wrap it in one before moving on to whatever "process" method you're calling.

respondido 09 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.