菜鸟教程小白 发表于 2022-12-12 12:13:23

ios - NSInvalidArgumentException 有一个字典


                                            <p><p>我正在尝试将 <code>NSDictionary</code> 数据放入 <code>tableView</code>。但是当只有一个值时我会遇到问题。</p>

<p>这里,有一个“上传”计数返回 8(但它应该返回 1),如果有多个“上传”计数返回“上传”的数量,因为它应该在第一种情况下。</p>

<pre><code>NSDictionary *jsonDictionary = aDicLog;
NSMutableArray *tableData = jsonDictionary[@&#34;multimedia&#34;][@&#34;uploads&#34;][@&#34;upload&#34;];

int count = ;
NSLog(@&#34;COUNT: %i&#34;, 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 &#39;NSInvalidArgumentException&#39;, reason: &#39;-: unrecognized selector sent to instance
</code></pre>

<p><strong>字典如下所示:</strong> </p>

<p>这是应用崩溃的时间:(只需一次“上传”)</p>

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

<p>这是应用运行良好的情况:(有多个“上传”)</p>

<pre><code>Printing description of tableData:
&lt;__NSArrayM 0x7a49dc90&gt;(
{
    FileName =   {
      text = &#34;1-FB5A9792.MOV&#34;;
    };
    status =   {
      text = 4;
    };
    Event =   {
      text = Test;
    };
    Date =   {
      text = &#34;9/7/2016 9:06:21 AM&#34;;
    };
    Publicacion =   {
      text = &#34;Example&#34;;
    };
    Seccion =   {
      text = Sistemas;
    };
    idMultimedia =   {
      text = 993;
    };
    text = &#34;&#34;;
},
{
    FileName =   {
      text = &#34;2-FB5A9793.MOV&#34;;
    };
    status =   {
      text = 4;
    };
    Event =   {
      text = Test;
    };
    Date =   {
      text = &#34;9/7/2016 9:06:21 AM&#34;;
    };
    Publicacion =   {
      text = &#34;Example&#34;;
    };
    Seccion =   {
      text = Sistemas;
    };
    idMultimedia =   {
      text = 994;
    };
    text = &#34;&#34;;
},...
</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>&lt;multimedia&gt;
&lt;uploads&gt;
    &lt;upload&gt;
      &lt;id&gt;1136&lt;/id&gt;
      &lt;Date&gt;9/9/2016 11:01:58 AM&lt;/Date&gt;
      &lt;FileName&gt;IMG_0510.MOV&lt;/FileName&gt;
      &lt;status&gt;2&lt;/status&gt;
      &lt;Publicacion&gt;Example&lt;/Publicacion&gt;
      &lt;Seccion&gt;Sistemas&lt;/Seccion&gt;
      &lt;Event&gt;Q&lt;/Event&gt;
    &lt;/upload&gt;
&lt;/uploads&gt;
&lt;/multimedia&gt;
</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[@&#34;multimedia&#34;][@&#34;uploads&#34;][@&#34;upload&#34;];
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]
查看完整版本: ios - NSInvalidArgumentException 有一个字典