从核心数据中删除项目

时间:2016-11-08 14:40:00

标签: swift xcode core-data swift3

我有一个功能,它会返回一些字符串

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)")
    }
}

正如您在我找到每个字符串(它是一个网址)后的评论中所看到的,我需要能够处理它然后删除它。

我可以正常处理我所拥有的内容,我只是不知道如何从核心数据中删除对象可以为我提供一些帮助吗?

1 个答案:

答案 0 :(得分:2)

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

context = getContext()

然后我就能够做到

context.delete(urls)