CGR CGR - 2 months ago 12
iOS Question

NSInvalidArgumentException having a single dictionary

I'm trying to put NSDictionary data into a table view. But I'm having problem when there is only one value.

Here, having one "upload" count returns 8 (but it should return 1) and if there is more than one "upload" count returns the number of "upload"'s as it should be in the first case.

NSDictionary *jsonDictionary = aDicLog;
NSMutableArray *tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];

int count = [tableData count];
NSLog(@"COUNT: %i", count);


Table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}


Cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//App Crashes in this line
NSDictionary *ldUpload = [tableData objectAtIndex:indexPath.row];
...
return cell;
}


Exception, Having one "upload":

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance


The dictionary looks like this:

Here is when the app crashes: (With just one "upload")

Printing description of tableData:
{
FileName = {
text = "IMG_0510.MOV";
};
status = {
text = 2;
};
Event = {
text = Q;
};
Date = {
text = "9/9/2016 11:01:58 AM";
};
Publicacion = {
text = "Example";
};
Seccion = {
text = Sistemas;
};
idMultimedia = {
text = 1136;
};
text = "";
}


Here is when the app works well: (With more than one "upload")

Printing description of tableData:
<__NSArrayM 0x7a49dc90>(
{
FileName = {
text = "1-FB5A9792.MOV";
};
status = {
text = 4;
};
Event = {
text = Test;
};
Date = {
text = "9/7/2016 9:06:21 AM";
};
Publicacion = {
text = "Example";
};
Seccion = {
text = Sistemas;
};
idMultimedia = {
text = 993;
};
text = "";
},
{
FileName = {
text = "2-FB5A9793.MOV";
};
status = {
text = 4;
};
Event = {
text = Test;
};
Date = {
text = "9/7/2016 9:06:21 AM";
};
Publicacion = {
text = "Example";
};
Seccion = {
text = Sistemas;
};
idMultimedia = {
text = 994;
};
text = "";
},...


I´m using XMLReader to convert an XML to Dictionary, then pass the above data to tableData

XML

<multimedia>
<uploads>
<upload>
<id>1136</id>
<Date>9/9/2016 11:01:58 AM</Date>
<FileName>IMG_0510.MOV</FileName>
<status>2</status>
<Publicacion>Example</Publicacion>
<Seccion>Sistemas</Seccion>
<Event>Q</Event>
</upload>
</uploads>
</multimedia>


How can i return 1 instead of 8 when only have 1 "upload" and n in case of n "upload"'s?

Answer

You can check the object type and if it is Dictionary or NSArray on the basis of that generate your tableData array.

id uploadsObj = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];
if ([uploadsObj isKindOfClass:[NSDictionary class]]) {
    NSMutableArray *tableData = [NSMutableArray arrayWithObject:(NSDictionary*)uploadsObj];
}
else if ([uploadsObj isKindOfClass:[NSArray class]]) {
    NSMutableArray *tableData = [NSMutableArray arrayWithArray:(NSArray*)uploadsObj];
}