how can I set an int to a value from array ?
I try like that:
int counter;
counter = signArr[4];
doesn't work, I get an err " incompatible pointer to integer conversion sending 'int' to parameter of type 'id' "
thank for help :)
From your comment we can guess that the number is boxed in an NSNumber instance. We can test with a log statement (or in the debugger):
NSLog(#"The class is %#", NSStringFromClass[signArr[4] class]));
and then, if it is an NSNumber instance, looking at the methods on offer we can see integerValue which will provide what you're looking for.
The error you saw
incompatible pointer to integer conversion sending 'int' to parameter of type 'id'
tells you that you want an int but that you have an id. You should know that id is the generic pointer type used in ObjC (if you don't you need to go and read the documentation some more) and thus you need to find a way to convert from the object pointer to the primitive int. The first step is finding out what the object class type is and then checking the docs to see what it offers you to help achieve your goal.
Related
When I convert an array element to an integer using this statement
int test=(int)[myArray objectAtIndex:2];
later use of "test" passed to other commands fails.However, this statement works
int test=[[myArray objectAtIndex2]intValue];
What is the difference between these two types of conversion?
The first is a cast. You're taking the object and casting it to an int, which will give you an int that contains the address of the object (and under 64-bit it will only contain the low 32 bits of the address). This isn't at all what you want.
The second is a method call for -intValue, which is implemented by NSNumber (and NSString) to return the int that the NSNumber (or NSString) object represents. This is (presumably) what you actually want.
The first statement is not a conversion. It is a cast of the pointer stored as element 2 in your array. The statement just assigns value of that pointer (casted to an int) to the variable.
The second statement is a converstion. It calls -intValue on object that pointer stored at that index 2 points to (possibly a NSNumber instance).
- intValue works on objects of type NSString * and NSNumber *.
If your object is actually #"123", or an [NSNumber numberWithInt:123] instead of just int 123, then using - intValue will convert it to 123. If you try and cast it directly using (int) and it's one of the above two types, then you'll run into errors accessing it as an int (because it's not, it's a more complex type).
I have a function that sets an entity within a Core Data store. I used to have all values it would be storing as type double, however now I must make it accommodate NSStrings as well. Consequently, I changed the type of the parameter the function takes in, to an id type. However, now I get the error:
error: incompatible type for argument 1 of 'numberWithDouble:'
...at the following lines:
//...
[dfm setTimeStamp:[NSNumber numberWithDouble:value]];
//...
[[fetchedObjects objectAtIndex:0] setValue:[NSNumber numberWithDouble:value] forKey:#"timeStamp"];
//...
Apparently it doesn't like the [NSNumber numberWithDouble:value] segment of each line. I was contemplating making a container class that holds an NSNumber type (doesn't Apple already have a class like this?) to get around this problem, but I thought that there has to be an easier way I am not thinking of (besides duplicating the function and changing the type of the value parameter). Any ideas? Thanks in advance!
EDIT:
Here is the function declaration:
-(void)setItemInDFMWhilePreservingEntityUniquenessForItem:(attribute)attr withValue:(id)value
attribute is merely an enum which specifies which entity to store within. The problem is that the compiler is giving me problems with value being of type id, theoretically I can pass in anything I want, and I believe the way I have it I am implying that I will be passing it as an NSNumber, but the compiler doesn't like that as that is not actually a class instance I suppose?
The problem is that the compiler is
giving me problems with value being of
type id, theoretically I can pass in
anything I want, and I believe the way
I have it I am implying that I will be
passing it as an NSNumber, but the
compiler doesn't like that as that is
not actually a class instance I
suppose?
By declaring value as id, you can pass any object you want. But why do you "suppose" that NSNumber isn't an object, when it's clearly documented as being an object? The warning isn't about passing an NSNumber instance when you've declared value as an id - that's perfectly valid, because id means "any object," and an NSNumber instance is an object. The warning comes from calling +numberWithDouble:, a method that takes a double for its first argument, and passing it value, which is declared as id - i.e. an object. You can't pass an object to a method that expects a double.
Your proposed solution, typecasting value with (NSInteger)value will silence the warning, but it won't fix the problem. The typecast simply converts the memory address the object pointer targets to an integer value. If (as your edit suggests) value is already an NSNumber object, what do you hope to gain by creating another one, or by typecasting its memory address to an integer? Just do:
[dfm setTimeStamp:value];
The problem lies with the value variable. It should be declared as a double (primitive) for this call to succeed.
edit: after rereading your question, do a check in the function on the type of value, if it is an NSString (use [value isKindOfClass:[NSString class]]) store it as such, if its not then its a double (if thats the only two types you are passing) and store it as such.
Can't you just pass the NSNumber instead of double?
Just realized that the call I was making (numberWithDouble:) was having the compiler check for a primitive, i.e. double. Changing it to the following worked like a charm:
[dfm setTimeStamp:[NSNumber numberWithInteger:(NSInteger)value]];
Thanks to those that responded!
I declare "extern NSDate *chooseDate" in my "global.h" file. I include this file in my "blueview" and in blueview I add the line "NSDate *chooseDate = YES". I get the warning message as shown in the title. I have read up on this, and I understand that the warning is saying that I am trying to assign an object to an integer. The part I don't get is that I am declaring chooseDate as an NSDate in both files. I don't understand how either is an integer. The funny part is that my program works correctly, I am just trying to get rid of this warning. Any help would be greatly appreciated.
You're reading the error backwards: It says you're trying to assign an integer to an object variable. So you're correct that the variable is an object pointer, but YES is not a pointer to an NSDate object, it is a BOOL value (which is a kind of integer).
I using something like this : [tmpArray insertObject:something[i] atIndex:i];
But I got a MSG : passing argument 1 of "insertObject:atIndex:" makes pointer from integer without a cast.
What should I do to fix it?
I guess your something[] array is a C array of int, if this is the case you cannot pass its value at that position into an NSArray since it contains objects (and objects in obj-C are referenced by pointers).
What you could do is wrap the int into a NSNumber this way:
[tmpArray insertObject:[NSNumber numberWithInt:something[i]] atIndex:i];
You can't magically make random pointer into an object, you need to understand what the type of the pointer is and do something semantically reasonable with that type. Also, that is not what the error you are seeing is about. The error is because the type expected is a pointer (and an object in ObjC is also a pointer type), and the type info of the thing you are passing is a scalar.
Taking a wild guess, something is probably an array of ints, if so what you want to do is wrap is the integer in an NSNumber in order to store it in a collection:
//assuming this:
int something[100];
//then your line should be this:
[tmpArray insertObject:[NSNumber numberWithInt:something[i]] atIndex:i];
Obviously you will need to unbox the value anywhere you access it. If it is not an array of integers then you will need to do something else (though probably similiar), but without more info I can't tell you exactly what.
I'm having trouble with this code:
NSRect itemFrame;
id item;
// code to assign item goes here.
itemFrame.origin.y -= [item respondsToSelector:#selector(selectedHeight)] ? [item selectedHeight] : [self defaultSelectedHeight];
This is the problematic bit:
[item selectedHeight]
The compiler is assuming that the return type is id. I though that adding a cast would fix this:
(float)[item selectedHeight]
but it doesn't work.
What am I doing wrong? (I suspect the problem is to do with resolving pointers related to id but I can't find any relevant documentation).
you want [[item selectedHeight] floatValue], assuming that the selectedHeight returns an NSNumber.
I know that below code works for iOS 6 SDK. I assume that obj object contains a float value.
id obj;
float fVal;
fVal = [obj floatValue];
You need to look at the declaration of your selectedHeight method. The problem is either that the method is returning a pointer to an object (id), or you haven't imported the header file for item in the file that contains the code snippet, so Xcode assumes it's a pointer by default.
You can't cast a pointer to a float, since they're fundamentally incompatible types. Once you get your declarations straightened out though you should be okay.
The compiler makes that kind of assumptions when multiple classes declare methods with the same name, that return different types. Since your "item" variable is typed as an "id," the compiler doesn't know which of these classes it will be sending the message to at run time, and chooses one.
To avoid this problem, you can inform the compiler what class "item" is an instance of, by declaring it with a specific type instead of the generic "id":
SomeItemClass *item;
You could also avoid it by not declaring identically-named methods that return different types.