Created
August 21, 2015 02:06
-
-
Save dbgrandi/37c3ac3a2ea9911db06e to your computer and use it in GitHub Desktop.
Swift public class with private var equality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let t1 = TinkerObject(id:1) | |
let t2 = TinkerObject(id:2) | |
// this will work | |
print (t1==t2) | |
// this would be an error: | |
// 'TinkerObject' does not have a member named 'id' | |
// print(t1.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TinkerObject : Equatable { | |
private let id:Int | |
init(id:Int) { | |
self.id = id | |
} | |
} | |
// | |
// note that this is OUTSIDE of your class impl to make it | |
// global, but INSIDE the same file to allow access to the | |
// private variable | |
// | |
public func ==(lhs: TinkerObject, rhs: TinkerObject) -> Bool { | |
return lhs.id == rhs.id | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Throw these in a directory and compile with
swiftc *.swift