Reputation: 23
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 ?
Thanks a lot :)
Upvotes: 1
Views: 828
Reputation: 5257
try this
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.
Upvotes: 1
Reputation: 2860
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"]
out as an 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.
Upvotes: 0