【问题标题】:Delete item from core data从核心数据中删除项目
【发布时间】:2016-11-08 14:40:00
【问题描述】:

我有一个返回一些字符串的函数

func getURLsToSend () {
    //create a fetch request, telling it about the entity
    let fetchRequest: NSFetchRequest<URLsToSend> = URLsToSend.fetchRequest()

    do {
        //Get results
        let searchResults = try getContext().fetch(fetchRequest)

        //You need to convert to NSManagedObject to use 'for' loops
        for urls in searchResults as [NSManagedObject] {
            //get the Key Value pairs (although there may be a better way to do that...
            print("\(urls.value(forKey: "url"))")

            //Process URL

            //Delete from Core Data

        }
    } catch {
        print("Error with request: \(error)")
    }
}

正如您在找到每个字符串(它是一个 url)后在 cmets 中看到的那样,我需要能够处理它然后删除它。

我可以处理我所拥有的一切,这很好我只是不知道如何从核心数据中删除对象,有人可以帮我解释一下吗?

【问题讨论】:

    标签: swift xcode core-data swift3


    【解决方案1】:

    意识到我的错误,我需要这样做

    context = getContext()
    

    然后我就可以简单地做

    context.delete(urls)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多