Last active
April 14, 2016 00:17
-
-
Save MarkVillacampa/74273386f1e2f006dfc639ef8d922c25 to your computer and use it in GitHub Desktop.
Very simple demonstration of NSHashTable in RubyMotion
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
class MyObject | |
attr_accessor :my_id | |
def isEqual(object) | |
object.class == self.class && object.my_id == my_id | |
end | |
def hash | |
my_id.hash | |
end | |
end | |
cache = NSHashTable.alloc.initWithOptions(NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality, capacity:0) | |
my_object = MyObject.new | |
my_object.my_id = 123 | |
cache.addObject(my_object) | |
p my_object | |
# => #<MyObject:0x7f9cf94b80e0 @my_id=123> | |
other_object = MyObject.new | |
other_object.my_id = 123 | |
cached_object = cache.member(other_object) | |
p cached_object | |
# => #<MyObject:0x7f9cf94b80e0 @my_id=123> | |
p cache.allObjects | |
# => [#<MyObject:0x7f9cf94b80e0 @my_id=123>] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment