objective c - iOS: Object equality when enumerating -


say have nsarray, , each item nsdictionary 3 keys keya, keyb, , keyc - each referring objects of unknown type (id).

if wanted write method found given element 3 keys i.e.

-(nsdictionary *) itemthatcontainskeys:(id)objecta and:(id)objectb and:(id)objectc 

would run trouble enumerating through , testing object equality via if([i objectforkey:(keya) isequalto:objecta]) etc? passing in actual objects set in dictionary initialization - ie not strings same value different locations.

is bad practise? there better way without creating database?

you can override isequal stipulate notion of equality type. same rules apply in other languages:

  • if provide implementation of equals should provide implementation of 'hash'
  • objects 'equal' should have same 'hash'
  • equals should transitive -> if equals b, , b equals c, c must equal a.
  • equals should bi-directional -> if equals b, b must equal a.

this ensure predictable behavior in classes nsset, use hash performance, falling equals on when there's collision.

as jason whitehorn notes, objective-c has convention of providing isequaltomytype method convenience.

appcode, equalsbuilder, boiler-plate code

it nice if there apache's 'equalsbuilder' class, in meantime appcode fine job of implementing these methods you.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -