ios - NSInvalidArgumentException 有一个字典
<p><p>我正在尝试将 <code>NSDictionary</code> 数据放入 <code>tableView</code>。但是当只有一个值时我会遇到问题。</p>
<p>这里,有一个“上传”计数返回 8(但它应该返回 1),如果有多个“上传”计数返回“上传”的数量,因为它应该在第一种情况下。</p>
<pre><code>NSDictionary *jsonDictionary = aDicLog;
NSMutableArray *tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];
int count = ;
NSLog(@"COUNT: %i", count);
</code></pre>
<p><strong>表格</strong></p>
<pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
}
</code></pre>
<p><strong>单元格</strong></p>
<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//App Crashes in this line
NSDictionary *ldUpload = ;
...
return cell;
}
</code></pre>
<p>异常(exception),有一个“上传”:</p>
<pre><code>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-: unrecognized selector sent to instance
</code></pre>
<p><strong>字典如下所示:</strong> </p>
<p>这是应用崩溃的时间:(只需一次“上传”)</p>
<pre><code>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 = "";
}
</code></pre>
<p>这是应用运行良好的情况:(有多个“上传”)</p>
<pre><code>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 = "";
},...
</code></pre>
<p>我正在使用 <a href="http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/" rel="noreferrer noopener nofollow">XMLReader</a>将 XML 转换为 <code>Dictionary</code>,然后将上述数据传递给 tableData</p>
<p><strong>XML</strong></p>
<pre><code><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>
</code></pre>
<p>当只有 1 个“上传”和 n 个“上传”的情况下,我如何返回 1 而不是 8?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以在生成tableData数组的基础上检查对象类型是<code>Dictionary</code>还是<code>NSArray</code>。</p>
<pre><code>id uploadsObj = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];
if (]) {
NSMutableArray *tableData = ;
}
else if (]) {
NSMutableArray *tableData = ;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - NSInvalidArgumentException 有一个字典,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/39728785/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/39728785/
</a>
</p>
页:
[1]