简体   繁体   中英

Why am I getting this compiler warning?

I'm getting a compiler warning when using the following code:

for(int i=0; i<[mutableTempArray count]; i++){
  //...
  [tempInfoDictionary setValue:i forKey:@"tag"];
  //...
}

The warning is:

passing argument 1 of 'setValue:forKey:' makes pointer from integer without a cast

What am I doing wrong here?

Collections in Objective-C can only hold objects , so you'll need to wrap i in an NSNumber object.

i itself is a primitive type, not an object, so it can't be put inside a collection object.

The first argument to setValue:forKey: is expected to be of pointer type, which i itself clearly isn't.

I suggest using the setObject:forKey: method on an NSDictionary instead of setValue:forKey: , because it states your intentions better. You're not only storing a value , you're storing an object

[tempInfoDictionary setObject:[NSNumber numberWithInt:i] forKey:@"tag"];

You're trying to put an int into an object array, but it's expecting an id ie a pointer. Try [tempInfoDictionary setValue:[NSNumber numberWithInt:i] forKey:@"tag"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM