具有单个字典的 NSInvalidArgumentException
NSInvalidArgumentException having a single dictionary
我正在尝试将 NSDictionary
数据放入 tableView
。但是当只有一个值时我遇到了问题。
在这里,有一个"upload"计数returns 8(但它应该return 1)如果有多个"upload"计数returns 第一种情况下应该是 "upload" 的数量。
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];
}
单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//App Crashes in this line
NSDictionary *ldUpload = [tableData objectAtIndex:indexPath.row];
...
return cell;
}
异常,有一个 "upload":
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance
字典是这样的:
这是应用程序崩溃的情况:(只有一个 "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 = "";
}
以下是该应用运行良好的情况:(超过一个 "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 = "";
},...
我正在使用 XMLReader 将 XML 转换为 Dictionary
,然后将上述数据传递给 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>
当只有 1 个 "upload" 和 n 个 "upload" 时,我如何 return 1 而不是 8?
Swift版本:
只需检查数据是字典还是数组:
var tableData:[[String:AnyObject]]! = []
if let data = jsonDictionary[@"multimedia"][@"uploads"][@"upload"] as? [String:AnyObject] {
print("Single upload data")
tableData.append(data)
} else {
if let data = jsonDictionary[@"multimedia"][@"uploads"][@"upload"] as? [[String:AnyObject]] {
print("multiple upload data")
tableData = data
}
}
您可以检查对象类型,如果它是 Dictionary
或 NSArray
,则在此基础上生成您的 tableData 数组。
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];
}
我正在尝试将 NSDictionary
数据放入 tableView
。但是当只有一个值时我遇到了问题。
在这里,有一个"upload"计数returns 8(但它应该return 1)如果有多个"upload"计数returns 第一种情况下应该是 "upload" 的数量。
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];
}
单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//App Crashes in this line
NSDictionary *ldUpload = [tableData objectAtIndex:indexPath.row];
...
return cell;
}
异常,有一个 "upload":
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance
字典是这样的:
这是应用程序崩溃的情况:(只有一个 "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 = "";
}
以下是该应用运行良好的情况:(超过一个 "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 = "";
},...
我正在使用 XMLReader 将 XML 转换为 Dictionary
,然后将上述数据传递给 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>
当只有 1 个 "upload" 和 n 个 "upload" 时,我如何 return 1 而不是 8?
Swift版本:
只需检查数据是字典还是数组:
var tableData:[[String:AnyObject]]! = []
if let data = jsonDictionary[@"multimedia"][@"uploads"][@"upload"] as? [String:AnyObject] {
print("Single upload data")
tableData.append(data)
} else {
if let data = jsonDictionary[@"multimedia"][@"uploads"][@"upload"] as? [[String:AnyObject]] {
print("multiple upload data")
tableData = data
}
}
您可以检查对象类型,如果它是 Dictionary
或 NSArray
,则在此基础上生成您的 tableData 数组。
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];
}