Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector
Objective-C does not support method overloading, you have to use a different method name. When you inherited UIViewController you inherited NSObject and made the class interopable to Obj-C. Swift on the other hand does support overloading, that's why it works when you remove the inheritance.
Method with Objective-C selector conflicts - 'with' at the end
#str
will be translated to withStr
in objc. You can use the @objc(someOtherSelectorName:)
annotation to specify a different selector name for objc and avoid the conflict.
Swift - Method '*()' with Objective-C selector '*' conflicts with getter for '*' from superclass 'UIView' with the same Objective-C selector
Try renaming the borderColor
variable name. It seems the ChartViewBase is using it.
Also please change all property names from your custom classes that are conflicting with your base class variable names and property names.
This should work.. Cheers!
Swift 2, method 'setOn' with Objective-C selector 'setOn:' conflicts with setter for 'on' with the same Objective-C selector
Similarly as in Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector, you can also hide properties from
the Objective-C runtime with @nonobjc
:
public class AView: UIView {
@nonobjc var on: Bool = false
public func setOn(on: Bool, animated: Bool) {
self.on = on
// do something more about animating
}
public func setOn(on: Bool) {
setOn(on, animated: false)
}
}
which prevents a conflicting Objective-C setter from being auto-generated.
Method 'Method()' with Objective-C selector 'Method' conflicts with getter for 'Method' with the same Objective-C Selector
You'll have to change the function name, or override those functions if that's what you want.
By the way, name conflicting should always be avoided. I suggest using something like myURL
, myCanGoBack()
.
Error declaring method with Objective-C selector
Selectors have changed with Xcode 7.3. You need to explicitly declare the selector that belongs to your class:
class MyClass {
func someFunc() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyClass.itemDroppedOnCharacter(_:)), name: "onTargetDropped", object: nil)
}
@objc func itemDroppedOnCharacter(notif: AnyObject) {
print("Item Dropped On Character")
}
}
You can also have MyClass
inherit from NSObject
if you don't want to add @objc
to expose the selector to Objective-C:
class MyClass: NSObject {
func someFunc() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyClass.itemDroppedOnCharacter(_:)), name: "onTargetDropped", object: nil)
}
func itemDroppedOnCharacter(notif: AnyObject) {
print("Item Dropped On Character")
}
}
Swift - Method '*()' with Objective-C selector '*' conflicts with getter for '*' from superclass 'NSObject' with the same Objective-C selector
To elaborate: @property(readonly) NSUInteger hash
is an Objective-C property of NSObject
, that means there is a getter created for that variable, namely hash()
.
You now try to define a method of the same name and the same parameters (none) but with a different return type (UInt
instead of NSUInteger
, which would be Int
in swift.). Therefore you receive the given error. To resolve that issue you have two options now:
- change the return type to
Int
-> that will override the predefined hash function - choose a different method name or add parameters
Related Topics
How Does One Generate a Random Number in Swift
How to Print the Type or Class of a Variable in Swift
How to Get Ip Address in Swift
What Is the "Some" Keyword in Swift(Ui)
Programmatically Detect Tab Bar or Tabview Height in Swiftui
Using a Type Variable in a Generic
Convert a Two Byte Uint8 Array to a Uint16 in Swift
Nsattributedstring, Change the Font Overall But Keep All Other Attributes
How to See Which Version of Swift I'M Using
Swift Structs to Nsdata and Back
What Is the 'Open' Keyword in Swift
Instance Member Cannot Be Used on Type