views:

234

answers:

3

I need to keep track of a bunch of objects that can be identified by two keys (Latitude and longitude actually, but there are some other properties). I need to do things like search for a particular object by those two keys, updating it if it is there and inserting if not. I was thinking. I was looking at NSDictionary and NSSet but thought I would hear what the masses have to say.

+4  A: 

I guess the simpler way is to use NSDictionary. You will be able to get your data by just doing [dic objectForKey:key].

Also, a good practice is to create some defines for the keys, so that it's easier to change a key name, and also avoids typo:

#define kObjectLatitude @"Latitude"
#define kObjectLongitude @"Longitude"

[object setObject:lat forKey:kObjectLatitude];
[object setObject:lon forKey:kObjectLongitude];

Don't forget to write the defines in a smart place. If you use it only in one class, just write them at the top of the declaration. If, however, you need them through different part of your code, you might consider moving it to the header file of the main class, or a specific header file for defines :)

NS(Mutable)Set will not be useful for you in this case. NSSets are mathematical sets, and you cannot access a specific data with a specific key (aka, you can't ask a set: "Hey, give me the longitude, where-ever you stored it!")

naixn
+1  A: 

Use NSDictionary. That's what it meant for.

unknownthreat
+2  A: 

This is not a direct answer, but a word of warning. Latitude and longitude are CLLocationDegrees, which is a double precision floating point value. Testing for equality on floats is a risky proposition since floating point math is inexact. You can easily have an equality test fail on two floats that should theoretically be equal. I don't know the requirements of your application, but you may want to test for proximity rather than equality.

cduhn
Good comment, thanks. I have definitely been bitten before when it comes to math operations with floats. I have been rounding them to a few significant digits and that seems to work so far. Will watch for this.
Codezy