-1

I'm trying to create an NSDictionary that keeps track of calling objects for a function. I'd like to create a unique string for each object without knowing anything about it. My first thought is to use the memory address of the object's pointer, but I'm not sure how to do that.

Any thoughts? I need to use some sort of unique id from an NSObject as the keys in my dictionary.

nothappybob
  • 3,097
  • 4
  • 16
  • 15
  • You posted a question that demanded unique keys, and then accepted an answer that has non-unique keys... Hmmmmmmm... You may want to edit your question, accept another answer, or write this off as deleted... – HackyStack Nov 15 '12 at 18:02

3 Answers3

2

If your application supports iOS6 only check the NSDictionaryOfVariableBindings macro.

The code would be something like :

    // Create the dictionary
    NSObject *firstObject  = [NSString stringWithString:@"My first item"];
    NSObject *secondObject = @"[@"an", @"array", @"of", @"strings"]";
    NSDictionary *theDic   = NSDictionaryOfVariableBindings(firstObject, secondObject);

    // Access data
    NSString *singleString  = [theDic objectForKey:@"firstObject"];
    NSArray  *listOfStrings = [theDic objectForKey:@"secondObject"];
Gammatora
  • 57
  • 8
0

My suggestion is not to use a dictionary. If you were to place them into an array, you could think of it as a dictionary with automatically generated unique keys (the indexes). It's really exactly what you are describing. If for some reason you have to use a dictionary, my suggestion is to implement that same model I'm speaking of, but you would have to generate and maintain the keys.

HackyStack
  • 4,887
  • 3
  • 22
  • 28
  • Can you explain this a little more? I think you are on to something. – nothappybob Nov 15 '12 at 17:36
  • It's simple. You want a dictionary with unique keys. If you use an array to store the objects, then the indexes in the array ARE your unique keys, and will be "automatically" generated for you by simply adding an object to the array. So your unique "key" for the 5th object you add would be '4', the first '0' and so on. You would obviously need to use an NSMutableArray for that setup, but then when you need the object whose unique "key" is 4 you simply call [myArray objectAtIndex:4]; It may sound overly simple, but it is, and that's not always a terrible thing... – HackyStack Nov 15 '12 at 17:40
  • I see what you are saying. The problem is, I need to be able to find those keys again based upon an NSObject. I don't want the NSObject to know anything about this implementation. I want to pass the NSObject in and keep a reference to it so I can keep track of what this NSObject has requested. – nothappybob Nov 15 '12 at 17:44
  • the object DOESN'T know about it's index in the array (the implementation -- it doesn't even know it's in an array), but if you need to determine the key from the object that implies the object MUST know something about the key or you can't determine it. When you say keep a reference, do you mean a pointer reference or could you store the index of the object (the key) as the reference to it? – HackyStack Nov 15 '12 at 17:50
  • "...that implies the object MUST know something about the key or you can't determine it." -- This is exactly the problem I am facing. The only way I could think of to handle this is determining a globally unique identifier from the object. That way, I can always recognize that object. The hash function mentioned in the accepted answer has done the trick. – nothappybob Nov 15 '12 at 18:02
-2

While I agree that your solution sounds like it may not be the best approach, have you considered -hash in the NSObject protocol? All NSObjects should return one. Be forewarned that it's a hash, so there's a chance that two different objects could have the same hash.

You could also consider a category on NSObject that your collection implements. The category could generate a UUID to use as a key.

kevboh
  • 5,207
  • 5
  • 38
  • 54
  • I believe this is the answer I was looking for. Thank you very much. I'm going to leave this question open for just a second, but I think this is what I am going to accept. – nothappybob Nov 15 '12 at 17:42
  • It's given me a start though. I think I'm going to try a combo of both things mentioned here. – nothappybob Nov 15 '12 at 18:05
  • Clarification: this answer has helped me with the approach I am taking. I didn't provide unique keys, I agree. – nothappybob Nov 15 '12 at 18:26
  • So when you have a duplicated key, how are you going to know which object you are going to get back? I don't even know what to say about that, it's a very interesting (read incredibly ignorant) approach. – HackyStack Nov 15 '12 at 19:56
  • @HackyStack I warned about the ramifications of using a hash function and provided an alternate solution that would return unique keys. Why the downvote? – kevboh Nov 15 '12 at 22:13
  • That's not the point of `-hash` at all. The pointer value is exactly what he is looking for. – user102008 Dec 01 '12 at 02:36