菜鸟教程小白 发表于 2022-12-11 19:03:37

ios - 试图快速从firebase数据库中获取用户


                                            <p><p>尝试快速从 firebase 数据库中获取用户,但我收到错误 <code>此类与键的键值编码不兼容</code>。我不认为我做错了什么,因为它不是我的第一个应用程序。我能够以完全相同的方式在其他应用程序中获取用户。但它现在不工作。所有必要的细节如下。请帮助,谢谢
这是代码</p>

<pre><code>func fetchUsers() {
    Database.database().reference().child(&#34;Users&#34;).observe(.childAdded, with: { (snapshot) in
      if let dictionary = snapshot.value as? {
            print(snapshot)
            let user = Users()
            user.setValuesForKeys(dictionary)
            //print(user.Email ?? &#34;&#34;)
            //print(user.Name ?? &#34;&#34;)

      }
    }, withCancel: nil)
}
</code></pre>

<p>这是模型:</p>

<pre><code>import UIKit

class Users: NSObject {
    var Name : String?
    var Email : String?
}
</code></pre>

<p>数据库截图
<a href="/image/R3F5t.png" rel="noreferrer noopener nofollow"><img src="/image/R3F5t.png" alt="enter image description here"/></a> </p>

<p>控制台出错</p>

<blockquote>
<p>ChatHub <strong>* Terminating app due to uncaught exception &#39;NSUnknownKeyException&#39;, reason: &#39;[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Email.&#39;
*</strong> First throw call stack:
(0x182dd7d38 0x1822ec528 0x182dd7a00 0x1836e3780 0x18377fc0c 0x1048000d8 0x104800174 0x1048ac760 0x1048d7e40 0x105b0d49c 0x105b0d45c 0x105b12050 0x182d7ff20 0x182d7dafc 0x182c9e2d8 0x184b2ff84 0x18c24b880 0x104806a64 0x1827c256c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) </p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在我看来,您不应该在开头使用大写字母命名您的实例/对象/变量,请参阅:<a href="https://softwareengineering.stackexchange.com/questions/202031/what-is-the-reason-for-using-lowercase-for-the-first-word-in-a-local-variable-e" rel="noreferrer noopener nofollow">https://softwareengineering.stackexchange.com/questions/202031/what-is-the-reason-for-using-lowercase-for-the-first-word-in-a-local-variable-e</a> .</p>

<p>回到您的问题,您是否尝试过重命名 <code>Users</code> 的属性?尽量使用<code>Struct</code>,不需要像这样使用<code>NSObject</code>的类和子类,方便初始化。我怀疑您的 <code>Email</code> 变量名称是导致错误的原因。</p>

<pre><code>struct Users {
    var name: String?
    var email: String?
}
</code></pre>

<p><code>Users</code> 的新实例:</p>

<pre><code>let user = Users(name: &#34;name here&#34;, email: &#34;email here&#34;)
</code></pre>

<p>我希望这会有所帮助!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 试图快速从firebase数据库中获取用户,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46877135/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46877135/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 试图快速从firebase数据库中获取用户