Skip to content

Instantly share code, notes, and snippets.

@dbgrandi
Created August 21, 2015 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbgrandi/37c3ac3a2ea9911db06e to your computer and use it in GitHub Desktop.
Save dbgrandi/37c3ac3a2ea9911db06e to your computer and use it in GitHub Desktop.
Swift public class with private var equality
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)
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
}
@dbgrandi
Copy link
Author

Throw these in a directory and compile with swiftc *.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment