迅速1.2覆盖NSManagedObject扩展中的prepareForDeletion [英] swift 1.2 override prepareForDeletion in NSManagedObject extension

查看:53
本文介绍了迅速1.2覆盖NSManagedObject扩展中的prepareForDeletion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 1.2中覆盖函数 prepareForDeletion 失败

Overriding the function prepareForDeletion fails in swift 1.2

// Playground - noun: a place where people can play

import UIKit
import CoreData

extension NSManagedObject {
    @objc func prepareForDeletion() {
        println("deleting object")
    }
}

错误:带有Objective-C选择器的方法'prepareForDeletion()''prepareForDeletion'与具有相同内容的先前声明冲突Objective-C选择器

Error: method 'prepareForDeletion()' with Objective-C selector 'prepareForDeletion' conflicts with previous declaration with the same Objective-C selector

@objc func prepareForDeletion() {
           ^

CoreData.NSManagedObject:31:14:注意:以前为'prepareForDeletion'在这里声明

CoreData.NSManagedObject:31:14: note: 'prepareForDeletion' previously declared here

@objc func prepareForDeletion()

有人有主意吗?

谢谢罗恩

推荐答案

不能覆盖同一类扩展中的类中的方法,这样做始终是未定义的行为.

You cannot override a method in a class in an extension of the same class, doing so was always undefined behaviour.

对于在Swift中覆盖Objective-C方法,在此未引起注意Xcode 6.2,现在可以在Xcode 6.3 beta中正确诊断.

For overriding Objective-C methods in Swift this went unnoticed in Xcode 6.2 and is now properly diagnosed in Xcode 6.3 beta.

请注意,Objective-C中的相应做法–同一类的Objective-C扩展中的重写方法–也不允许,请参阅避免类别方法名称冲突" :

Note that the corresponding practice in Objective-C – overriding methods in an Objective-C extension of the same class – is also not allowed, see "Avoid Category Method Name Clashes":

如果在类别中声明的方法的名称与方法的名称相同原始类中的方法,或同一类别中另一个类别中的方法类(甚至是超类),行为是不确定的方法实现在运行时使用.这不太可能是如果您在自己的班级中使用类别,则会出现问题,但可能会导致使用类别向标准可可粉添加方法时出现问题可可触控课程.

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause problems when using categories to add methods to standard Cocoa or Cocoa Touch classes.

您可以做的是在您的自定义方法中覆盖该方法 NSManagedObject 子类.

What you can do is to to override the method in your custom NSManagedObject subclasses.

这篇关于迅速1.2覆盖NSManagedObject扩展中的prepareForDeletion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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