Geminium
Geminium

Reputation: 1

Error on setObject with NSMutableDictionary

I'm new in Objective-C, I would like to pupulate a dictionary but i have an error on the line :

[fullCollaboraeur setObject:(id)tmpArray forKey:(id)col.Id];

This is the full code :

NSMutableArray *collaborateurs = [[NSMutableArray alloc] init];
...
NSMutableDictionary *fullCollaboraeur = [[NSMutableDictionary alloc] init];
for (Collaborateur *col in  collaborateurs)
 {
  NSMutableArray *tmpArray;
  tmpArray = [fullCollaboraeur objectForKey:(id)col.Id];
  if(tmpArray == nil)
  {
   NSLog(@"Value for %i is null", col.Id);
   tmpArray = [[NSMutableArray alloc] init];
   [tmpArray addObject:col];
   [fullCollaboraeur setObject:(id)tmpArray forKey:(id)col.Id];
  }
  else
  {
   NSLog(@"%i", col.Id);
   [tmpArray addObject:col];
   [fullCollaboraeur setObject:tmpArray forKey:(id)col.Id];
  }
 }

Can you help me please ?

Many thanks

Upvotes: 0

Views: 808

Answers (1)

bbum
bbum

Reputation: 162712

    NSLog(@"Value for %i is null", col.Id); ....     [fullCollaboraeur setObject:(id)tmpArray forKey:(id)col.Id];

If Id is of type (int), it can't be a key in a dictionary directly. Dictionaries take objects as keys.

Note:

  • Type casting is to be avoided. All those (id)s need to go away. If the compiler is giving a warning on those lines, it is probably indicating a real problem that isn't going to be fixed by type casting.

  • instance variables are named with a lowercase letter. id is a reserved word and, thus, should not be used as an instance variable name. Use something like columnIdentifier.

Upvotes: 5

Related Questions