我正在开发一个应用程序,需要检查作业的截止日期.我想知道截止日期是否在下周内,如果是,则执行操作.
我能找到的大部分文档都在Objective-C中,我无法弄清楚如何在Swift中完成它.谢谢您的帮助!!
我正在为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
}
Run Code Online (Sandbox Code Playgroud)
我写的确是这样,但Xcode给出了我的这些错误:
Consecutive declarations on a line must be separated by ';'
Expected declaration operators are only allowed at global scope
Run Code Online (Sandbox Code Playgroud)
我从apple的文档中找到了这段代码:https://developer.apple.com/documentation/swift/equatable
这和我写的很相似.怎么了?这对我来说似乎是个错误.我正在使用Xcode 6 Beta 2
这是我的整个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 …Run Code Online (Sandbox Code Playgroud) 当我实现Hashable协议时.需要在类外定义一个等同的协议函数,如下所示.如下.
func ==(lhs: Swap, rhs: Swap) -> Bool {
return (lhs.cookieA == rhs.cookieA && lhs.cookieB == rhs.cookieB) ||
(lhs.cookieB == rhs.cookieA && lhs.cookieA == rhs.cookieB)
}
class Swap: Printable,Hashable {
var cookieA: Cookie
var cookieB: Cookie
init(cookieA: Cookie, cookieB: Cookie) {
self.cookieA = cookieA
self.cookieB = cookieB
}
var hashValue: Int {
return cookieA.hashValue ^ cookieB.hashValue
}
var description: String {
return "swap \(cookieA) with \(cookieB)"
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说有点奇怪.在上面的例子中,func ==应该属于Swap类.那么为什么我们需要声明类外的func ==?