使用Swift 3删除核心数据托管对象 [英] delete core data managed object with Swift 3

查看:88
本文介绍了使用Swift 3删除核心数据托管对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 3迁移了我的代码并进行了更改:

Swift 3 has migrated my code and changed:

 context.deleteObject(myManagedObject)

 context.delete(myManagedObject)

这很好编译(XCode 8b3),但是在运行时抱怨上下文没有函数/选择器删除(managedObject)

this is compiling fine (XCode 8b3) but at runtime complaining that the context does not have a function/selector delete(managedObject)

这是运行时错误:

[NSManagedObjectContext delete: ]:无法识别的选择器已发送到实例

我的代码非常基本:

func delete()
{
    let appDel: AppDelegate = UIApplication.shared().delegate as! AppDelegate

    if let context: NSManagedObjectContext = appDel.managedObjectContext
    {
        context.delete(exerciseData)
        appDel.saveContext()
    }
}

为什么不再起作用?

谢谢

Greg

推荐答案

来自Xcode 8 beta 3-发行说明

From the Xcode 8 beta 3 - Release Notes

Xcode 8 beta 3中的已知问题– Swift编译器

Known Issues in Xcode 8 beta 3 – Swift Compiler


尝试使用NSManagedObjectContext的delete( :)方法可能会导致调用如果参数是可选的(包括ImplicitlyUnwrappedOptional),则在NSObject上添加UIKit的delete( :)方法(属于UIResponderStandardEditActions类别)。 (27206368)

Attempting to use NSManagedObjectContext's delete(:) method may result in calling the UIKit-added delete(:) method on NSObject instead (part of the UIResponderStandardEditActions category) if the argument is optional (including ImplicitlyUnwrappedOptional). (27206368)

解决方法:使用if let或!手动解开可选值。

Workaround: Manually unwrap the optional value using if let or !.

您需要检查您的情况是否成立。

You need to check if this holds true in your case.

这篇关于使用Swift 3删除核心数据托管对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆