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

ios - swift ios如何检查核心数据库中的字段


                                            <p><p>我如何检查我的核心数据库中的字段,如果我想添加一个实体,怎么可能?下面是我保存数据的代码,我想使用键杂货项目将除名称之外的其他数据添加到我的核心数据库表中。</p>

<pre><code> func saveItemInLocalDB(groceryItem : Item) {
      let context = getContext()

      //retrieve the entity that we just created
      let entity =NSEntityDescription.entity(forEntityName: &#34;GroceryItem&#34;, in: context)

      let item = NSManagedObject(entity: entity!, insertInto: context)

      //set the entity values
      item.setValue(groceryItem.name, forKey: &#34;name&#34;)
      item.setValue(false, forKey: &#34;isSelected&#34;)
      item.setValue(loggedInUserHouseNumber, forKey: &#34;houseNo&#34;)

      //save the object
      do {
            try context.save()
            print(&#34;ang item:&#34;, groceryItem.name)
            print(&#34;saved!&#34;)
      } catch let error as NSError{
            print(&#34;Could not save \(error), \(error.userInfo)&#34;)
      } catch {

      }
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以试试这个</p>

<pre><code>   let IsFeildAvailable = entity.attributesByName.keys.contains(&#34;keyname&#34;)

    if IsFeildAvailable
    {
      print(&#34;Key/field is exist&#34;)

    }else{
      print(&#34;Key/field does not exist&#34;)
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - swift ios如何检查核心数据库中的字段,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49570072/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49570072/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - swift ios如何检查核心数据库中的字段