Swift - 使用Objective-C选择器'*'的方法'*()'与来自超类'NSObject'的'*'的getter冲突,具有相同的Objective-C选择器

时间:2023-01-15 18:38:32

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

我将此xcode更新为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_);
    }

Please advice. Thank you.

请指教。谢谢。

2 个解决方案

#1


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().

详细说明:@property(readonly)NSUInteger哈希是NSObject的Objective-C属性,这意味着为该变量创建了一个getter,即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:

您现在尝试定义一个具有相同名称和相同参数(无)但具有不同返回类型的方法(UInt而不是NSUInteger,它将是swift中的Int。)。因此,您收到给定的错误。要解决该问题,您现在有两个选择:

  • change the return type to Int -> that will override the predefined hash function
  • 将返回类型更改为Int - >将覆盖预定义的哈希函数

  • choose a different method name or add parameters
  • 选择不同的方法名称或添加参数

#2


See the NSObjectProtocol declaration, where hash is declared:

请参阅声明哈希的NSObjectProtocol声明:

var hash: Int { get }

You have three problems:

你有三个问题:

  • hash is a var, not a func
  • hash是一个var,而不是一个func

  • the type is Int, not UInt.
  • 类型是Int,而不是UInt。

  • you didn't use the override keyword
  • 你没有使用override关键字

To resolve these issues, use this instead:

要解决这些问题,请改用:

override var hash : Int {
    return /* (your hash logic) */
}

#1


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().

详细说明:@property(readonly)NSUInteger哈希是NSObject的Objective-C属性,这意味着为该变量创建了一个getter,即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:

您现在尝试定义一个具有相同名称和相同参数(无)但具有不同返回类型的方法(UInt而不是NSUInteger,它将是swift中的Int。)。因此,您收到给定的错误。要解决该问题,您现在有两个选择:

  • change the return type to Int -> that will override the predefined hash function
  • 将返回类型更改为Int - >将覆盖预定义的哈希函数

  • choose a different method name or add parameters
  • 选择不同的方法名称或添加参数

#2


See the NSObjectProtocol declaration, where hash is declared:

请参阅声明哈希的NSObjectProtocol声明:

var hash: Int { get }

You have three problems:

你有三个问题:

  • hash is a var, not a func
  • hash是一个var,而不是一个func

  • the type is Int, not UInt.
  • 类型是Int,而不是UInt。

  • you didn't use the override keyword
  • 你没有使用override关键字

To resolve these issues, use this instead:

要解决这些问题,请改用:

override var hash : Int {
    return /* (your hash logic) */
}