简体   繁体   中英

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

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_);
    }

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

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) */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM