ios Swift Equatable 协议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24467960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift Equatable Protocol
提问by Addison
I was folling this tutorial for Swift: https://www.raywenderlich.com/125311/make-game-like-candy-crush-spritekit-swift-part-1and came across this code:
我正在关注 Swift 的本教程:https: //www.raywenderlich.com/125311/make-game-like-candy-crush-spritekit-swift-part-1并遇到了以下代码:
func == (lhs: Cookie, rhs: Cookie) -> Bool {
return lhs.column == rhs.column && lhs.row == rhs.row
}
I wrote exactly that, but Xcode is giving my these errors:
我正是这样写的,但 Xcode 给了我这些错误:
Consecutive declarations on a line must be separated by ';'
Expected declaration operators are only allowed at global scope
I found this code from apple's documentation: https://developer.apple.com/documentation/swift/equatable
我从苹果的文档中找到了这段代码:https: //developer.apple.com/documentation/swift/equatable
Which is very similar to what I wrote. Whats wrong? This seems like a bug to me. I am using Xcode 6 Beta 2
这与我写的非常相似。怎么了?这对我来说似乎是一个错误。我正在使用 Xcode 6 Beta 2
EDIT:
编辑:
This is my whole Cookie class:
这是我的整个 Cookie 课程:
class Cookie: Printable, Hashable {
var column: Int
var row: Int
let cookieType: CookieType
let sprite: SKSpriteNode?
init(column: Int, row: Int, cookieType: CookieType) {
self.column = column
self.row = row
self.cookieType = cookieType
}
var description: String {
return "type:\(cookieType) square:(\(column),\(row))"
}
var hashValue: Int {
return row * 10 + column
}
func ==(lhs: Cookie, rhs: Cookie) -> Bool {
return lhs.column == rhs.column && lhs.row == rhs.row
}
}
回答by Connor
Move this function
移动这个功能
func == (lhs: Cookie, rhs: Cookie) -> Bool {
return lhs.column == rhs.column && lhs.row == rhs.row
}
Outside of the cookie class. It makes sense this way since it's overriding the == operator at the global scope when it is used on two Cookies.
在 cookie 类之外。这是有道理的,因为当它用于两个 Cookie 时,它会在全局范围内覆盖 == 运算符。
回答by Anish Parajuli ?
SWIFT 2:
快速 2:
As in swift 2 NSObject
already conforms to Equatable
.You don't need conformance at the top so it's like
正如在 swift 2 中NSObject
已经符合Equatable
.You 不需要在顶部符合所以它就像
class Cookie: NSObject {
...
}
And you need to override isEqual
method as
并且您需要将isEqual
方法覆盖为
class Cookie:NSObject{
var column: Int
var row: Int
//..........
override func isEqual(object: AnyObject?) -> Bool {
guard let rhs = object as? Cookie else {
return false
}
let lhs = self
return lhs.column == rhs.column
}
}
This time isEqual
method is inside the class. :)
这个时间isEqual
方法在类内部。:)
EDIT for SWIFT 3:Change this method as
编辑 SWIFT 3:将此方法更改为
override func isEqual(_ object: AnyObject?) -> Bool {
guard let rhs = object as? Cookie else {
return false
}
let lhs = self
return lhs.column == rhs.column
}
回答by Felipe Ignacio Noriega Alcaraz
making the class an NSObject solved the equatable problems for me...
使类成为 NSObject 为我解决了等价问题...
class Cookie: NSObject {
...
}
(got the tip from the iOS apprentice tutorials)
(从 iOS 学徒教程中得到提示)