问题
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