Skip to content

Instantly share code, notes, and snippets.

@MarkVillacampa
Last active April 14, 2016 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkVillacampa/74273386f1e2f006dfc639ef8d922c25 to your computer and use it in GitHub Desktop.
Save MarkVillacampa/74273386f1e2f006dfc639ef8d922c25 to your computer and use it in GitHub Desktop.
Very simple demonstration of NSHashTable in RubyMotion
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