Incompatible integer to pointer conversion assigning to 'int *' from 'int' - ios

I have yet another pesky warning I would like gone. Basically, I have an int declared like this: #property (nonatomic, assign) int *myInt; and set like this: myInt = 0;. It is also synthesized in the implementation file. I am getting a warning on the line where I set the int's value and it says Incompatible intiger to pointer conversion assigning to 'int *' from 'int'. What should I do to fix this?

There's a big hint in the error message!
In C and Objective C, an int is a primitive data type. You've written int *, which means "a pointer to an int", whereas it looks like you just wanted an int.
So change your property to this:
#property (nonatomic, assign) int myInt;
For more info, google "C pointers" and you'll find information like this: http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Yes, simply remove asterisk (*) from declaration.
for example.declare BOOL isChecked instead of BOOL * isChecked.

int *MyInt is a pointer to int, not an int.
As others told you, just remove the * and you will have a regular int.

Another way to resolve this is to simply not declare a pointer (drop the asterisk):
#interface ViewController : UIViewController {
NSUInteger myObjcInt; // unsigned ( >= 0 ) NSObject int, yo
}

Related

Sending 'double' to parameter of incompatible type 'id _Nullable'

So I am unable to understand this error.
I have an object with these properties
#property (nonatomic, readwrite) double width;
#property (nonatomic, readwrite) double height;
When I am creating an NSSdictionary add adding them into it
if (config.width) {
properties[#"height"] = config.height;
}
if (config.height) {
properties[#"height"] = config.height;
}
I am getting following error
Sending 'double' to parameter of incompatible type 'id _Nullable'
If I am correct, type id is equivalent to any? which includes double. Also, I am setting value if it exist?
If I am correct, type id is equivalent to any? which includes double
But you're not correct. id is equivalent to any object (an NSObject subclass). double is not an object; it's a primitive.
Just in case if anyone wants to know how I was able to fix it, I did
[NSNumber numberWithDouble:config.height];

Is there a way to Make NSInvocation surport variable parmas function line [NSstring stringWithFormat:..]

Apple doc says "NSInvocation does not support invocations of methods with either variable numbers of arguments or union arguments. "
i searched for hours ,some people says var_list works, but i tryed ,it does Not
but I think there may be a way to do the same thing (reflection) on variable params function,as i metioned ,[stringWithFormat:],
so , I found a way ,please readt the code bellow .
#interface INTObj : NSObject
#property (nonatomic, assign) int realvalue
#end
#interface FloatObj : NSObject
#property (nonatomic, assign) float realvalue
#end
// here ,the selectorName is only know in runtime
NSString *selectorName = #"stringWithFormat:";
SEL selector = NSSelectorFromString(selectorName);
typedef id (*Obj_Imp)(id,SEL,...);
Method md = class_getClassMethod(obj, selector); // here, obj means NSString
Obj_Imp fun = (Obj_Imp )method_getImplementation(md); // stringWithFormat:...
NSString *arg1 = #"hello %f %d";
FloatObj *fo = [[FloatObj alloc] init];
fo.realvalue = 11.3;
INTObj *io = [[INTObj alloc] init];
io.realvalue = 5;
NSObject *arr[3] = {arg1, fo,io};
// it cracks . exc_bad_Access code = 1
NSString *result = fun(obj , selector, arr[0], [arr[1] realvalue],[arr[2] realvalue]) ;
// it works but i cant do this ,because i don't know the type of the 4th parameters at Runtime
NSString *result = fun(obj , selector, arr[0],[(FloatObj *)arr[1] realvalue],[arr[2] realvalue])
why does the second calling of the function "fun" works while the first one cracks?
is there a better way to to do this?
This has nothing to do with NSInvocation or calling the method implementation directly. You should get the same undefined behavior if you called the method directly:
NSString *result = [NSString stringWithFormat:arr[0], [arr[1] realvalue], [arr[2] realvalue]];
or even a regular function:
NSLog(arr[0], [arr[1] realvalue], [arr[2] realvalue]);
In C (and Objective-C) every expression must have a compile-time type. The compiler needs to be know this compile-time type be able to compile the code correctly.
So let me ask you, what should be the compile-time type of [arr[1] realvalue]? Is it int? Is it float? The compiler will do different things depending on what type it is. If it is float for example, the C standard says that float passed to varargs will be promoted to double. The calling conventions for passing an int and a double in varargs are different (in fact, these two types have different sizes). And +[NSString stringWithFormat:] expects the compile-time type of the thing you pass to match the format specifier you give it in your format string, or there will be undefined behavior.
From your format string, it seems like you wanted the [arr[1] realvalue] argument to be float or double since you used %f. Since FloatObj is the class whose realvalue method returns float, it seems that casting arr[1] to FloatObj * is the right thing to do.

How to understand BOOL *

Like in this NSArray instance method enumerateObjectsUsingBlock:^(id x, NSUInteger index, BOOL *stop),as I know BOOL is a primitive type,How can we declare it as a pointer type? why it's not BOOL stop here?
You can wrap other non-object types (such as a pointer or a struct) in an NSValue.
Assuming you really mean a BOOL* (pointer):
NSValue *boolValue = [NSValue value:pointerToBool withObjCType:#encode(BOOL*)];
BOOL *b = [boolValue pointerValue];
Because you're defining stop as a pointer to a BOOL, not as a BOOL itself.
It lets you do things like pass-by-reference emulation in C and Obj-C:
void changeMe (BOOL *pointerToBool) {
*pointerToBool = ! (*pointerToBool); // Dereference the address
// to get at variable.
}
:
BOOL myBool = YES;
changeMe (&myBool); // Pass its address.
BOOL * is pointer of BOOL var. If use it (a pointer) in function argument you can change value of this pointer if you want. Use pointer in argument as a void function, but you can return one or more value in argument
It will help you knowing that BOOL is a signed char, so it can be treated as a it. It's declared in objc.h: http://www.opensource.apple.com/source/objc4/objc4-371.1/runtime/objc.h
The declaration:
typedef signed char BOOL;
#define YES (BOOL)1
#define NO (BOOL)0
BOOL* is pretty much the same as char* , and I'm sure you are more used to see the last.

Incompatible pointer to integer conversion assigning to 'int' from 'id'

i am getting this warning "Incompatible pointer to integer conversion assigning to 'int' from 'id'"
on this line
_intvalue = [self performSelector:#selector(PostureValueReturn)];
here abc is a method
- (int)abc
{
return 3;
}
and _intvalue is property
#property (nonatomic, assign) int intValue;
can anyone suggest me how to get rid of that .
The -performSelector method returns type id. Is there any reason you cannot actually call the method directly?
_intvalue = [self PostureValueReturn];

Incompatible inter to pointer conversion

The following code generates compiler warning (below the code)
NSUInteger positionat = [_bhkButtons indexOfObject:sender];
BOOL val = (BOOL) [_searchModel.BHkNo objectAtIndex:positionat];
val = !val;
[_searchModel.BHkNo insertObject:val atIndex:positionat];
Incompatible integer to pointer conversion 'BOOL' (aka 'signed char') to parameter of type 'id'
I'm a newbie to Objective - C. Please help.
Since BOOL is a primitive data type and arrays take in objects, you have to box/wrap the BOOL variable. You can use the NSNumber class for the same as shown below.
[_searchModel.BHkNo insertObject:[NSNumber numberWithBool:val] atIndex:positionat];

Resources