swift 1.2 override prepareForDeletion in NSManagedObject extension

孤者浪人 提交于 2020-01-06 15:14:59

问题


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

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: note: 'prepareForDeletion' previously declared here

@objc func prepareForDeletion()

Does anyone have an idea?

thanks Ron


回答1:


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

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

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.

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



来源:https://stackoverflow.com/questions/29528099/swift-1-2-override-preparefordeletion-in-nsmanagedobject-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!