Ios NSUinteger to enter 'id'

My brain is failing today. I know this should be easy, but I just don't see it.

CGFloat *minutes = [self.displayData objectForKey:index]; 

Incompatible integer to convert the pointer sending 'NSUInteger' (aka 'unsigned int') to a parameter of type 'id'

index is NSUInteger in the loop, (0 1 2 3, etc.)

How do I get past this? Thanks.

+4
source share
4 answers

The dictionary waits for the object as a key ( id ), not a simple integer ( NSUInteger ).

Try wrapping the integer in an NSNumber object.

 CGFloat *minutes = [self.displayData objectForKey:[NSNumber numberWithUnsignedInteger:index]]; 
+7
source

The -objectForKey: method returns a value of type id , but you are trying to assign it to CGFloat *, which is incompatible with id , because CGFloat is not a class. If you know that the object you are retrieving from the dictionary is a specific type, say, NSNumber, you can take steps to convert it to CGFloat when you receive it.

Also, you are trying to use int as a key in a dictionary, but dictionaries require their keys to be objects too. If you want to access your objects by index, save them in an array instead of a dictionary.

Putting it all together, you will have something like this:

 // after changing the displayData to an array NSNumber *number = [self.displayData objectAtIndex:index]; CGFloat minutes = [number floatValue]; 
+2
source

You cannot use this pointer:

 CGFloat minutes = [self.displayData objectForKey:index]; 
0
source

Easy question. Just do not use NSUIntegrer. Instead, do the following:

 id index;//then set the index 
-3
source

Source: https://habr.com/ru/post/1397491/


All Articles