3

I am struck in this warning for many hours... Am getting

The code is,

-(NSMutableDictionary*)iOSDiction
        {
            NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] initWithCapacity:[m_coordDic count]];
            for (NSObject *key in [m_coordDic allKeys]) 
            {
                [tmpDic setObject:[m_coordDic objectForKey:key] forKey:key];  //Warning
            }
            return tmpDic;
        }

Warning :

"Sending 'NSObject *' to parameter of incompatible type 'id'

Passing argument to parameter aKey here

NSDictionary.h

- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
AC Mohan
  • 33
  • 1
  • 4

4 Answers4

5

NSDictionary keys should conform to NSCopying protocol. It is a basic requirement of a key as specified in the NSDictionary reference. In general, NSObject does not conform to the protocol(its subclasses might), so you are getting a warning. The correct way to do this is

NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] initWithCapacity:[m_coordDic count]];
for (NSObject<NSCopying> *key in [m_coordDic allKeys]) 
{
    [tmpDic setObject:[m_coordDic objectForKey:key] forKey:key];  //Warning
}
return tmpDic;

This ensures your key is any object confirming to NSCopying protocol.

EDIT: It seems what you are really trying to do is simply [m_coordDic mutableCopy], a shallow copy. Any reason you are not doing this?

Roshan
  • 1,937
  • 1
  • 13
  • 25
3

NSObject doesn't conform to NSCopying. By typing the variable as NSObject, you're telling the compiler to make sure that any messages you send through that variable are responded to by NSObject. Since NSObject doesn't conform to NSCopying, you get that error message.

If you're trying to tell the compiler that a variable is some kind of object, but never mind the type, you should type it as id. So:

for (id key in m_coordDic)

(BTW, you don't need the allKeys. Fast enumeration already enumerates keys.)

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

It is due to the incompatible Assignment. So you should use Particular object type Or id--> for any instances of object.

Try this :

 for (id key in [arr allKeys])
    {
        [tmpDic setObject:[m_coordDic objectForKey:key] forKey:key];  //Warning
    }
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • I downvoted initially because the original answer was wrong (something about how you shouldn't pass a pointer). Then you edited and I tried to remove the downvote but SO isn't letting me. – Chuck Jan 31 '14 at 06:23
  • @Chuck Thanks, Yes, it was wrong with my assumption(Explanation). So i removed that part only . AnyHow thanks for your propose. – Kumar KL Jan 31 '14 at 06:25
1

NSDictionary keys must conform to the NSCopying protocol. That's what id<NSCopying> means.

Clearly, you are already working with NSDictionary keys that do conform to NSCopying, but you've declared your key variable as NSObject*, which by itself does not conform to NSCopying.

You need to declare that key does conform to NSCopying. You can do that with NSObject<NSCopying>* key; however, many people would use id instead.

id is a special type that tells the compiler to skip any type checking. In a simple case like this, using id is usually fine:

for (id key in [myDictionary allKeys])
{
    // etc...
}
Darren
  • 25,520
  • 5
  • 61
  • 71