Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Method '*()' with Objective-C selector '*' conflicts with getter for '*' from superclass 'NSObject' with the same Objective-C selector

Tags:

ios

swift

xcode6

I got this error message since update my xcode to 6.3.1.

/Users/MNurdin/Documents/iOS/xxxxx/Models/Message.swift:46:10: Method 'hash()' with Objective-C selector 'hash' conflicts with getter for 'hash' from superclass 'NSObject' with the same Objective-C selector

My code

var hash_ : UInt

func hash() -> UInt {
        return UInt(hash_);
    }
like image 319
Nurdin Avatar asked Feb 10 '23 06:02

Nurdin


2 Answers

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
like image 119
luk2302 Avatar answered Feb 13 '23 05:02

luk2302


See the NSObjectProtocol declaration, where hash is declared:

var hash: Int { get }

You have three problems:

  • hash is a var, not a func
  • the type is Int, not UInt.
  • you didn't use the override keyword

To resolve these issues, use this instead:

override var hash : Int {
    return /* (your hash logic) */
}
like image 40
Aaron Brager Avatar answered Feb 13 '23 06:02

Aaron Brager