Incompatible inter to pointer conversion - ios

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];

Related

Incompatible pointer types initializing 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') with an expression of type 'NSString *'

Hi I am learner in Objective-c Having a warning of Incompatible pointer types initializing 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') with an expression of type 'NSString *'
- (void)stopAnimating {
pause = YES;
if (timerArray) {
for (NSInteger i = 0; i < [timerArray count]; i++) {
dispatch_source_t _timer = [[timerArray objectAtIndex:i] source];
dispatch_source_cancel(_timer);
_timer = nil;
}
timerArray = nil;
}
[self removeAllFlakesWithAnimation:YES];
}
in dispatch_source_t _timer = [[timerArray objectAtIndex:i] source]; this line, how to solve, timeArray is a NSMutableArray NSMutableArray *timerArray;
We can't tell you what is wrong with your code, there is not enough information for that, but we can tell you what the compiler is doing and why it produces the error it does – then you'll have to resolve it from there.
In your line:
dispatch_source_t _timer = [[timerArray objectAtIndex:i] source];
The LHS declares are variable, _timer, of type dispatch_source_t so the RHS needs to return a value of this type. Let's look at the RHS:
[timerArray objectAtIndex:i]
which BTW you can write more succinctly as:
timerArray[i]
this indexes into an array which you have declared as:
NSMutableArray *timerArray;
the elements of an array like this have type id – which means a reference to any object. The actual type of the objects in the array in this case will not be known until runtime. The next part of the RHS is:
[<a reference so some object> source]
Objective-C allows this and will perform a check at runtime to determine that the reference object does indeed have a method source. However at compile time the compiler can look up the definition of methods called source, it does, and finds that the method returns an NSString *.
So the RHS returns an NSString * and the LHS requires an dispatch_source_t and therefore the compiler reports:
Incompatible pointer types initializing 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') with an expression of type 'NSString *'
Now you have to figure out whether you intended to call source or some other method which does return a value of the right type, etc. HTH
As another BTW to someone learning Objective-C: You are using a for loop to produce an index value for an array, and you only use that value to index the array once. A better way to do this is to use a for/in loop:
for (<YourObjectType> element in timerArray) {
dispatch_source_cancel([element source]);
}
You need to replace <YourObjectType> with the type of object references you've stored in timerArray, and as above the source method needs to return a dispatch_source_t value.
Objective-C has a for ... but there are other really nice ways in which you can iterate through elements of an array. I give one example for array a, in this case of NSString *
[a enumerateObjectsUsingBlock: ^ ( NSString * i, NSUInteger idx, BOOL * stop ) {
// do something with NSString * i
// its index into the array is idx if you need it
// to exit out of the loop do
* stop = YES;
}];

Pass a variable to decimalNumberHandlerWithRoundingMode?

Forgive me if I use the wrong terminology as I'm still a little new at iOS development. I've built a calculator-type app and I want users to be able to control how numbers are rounded. Here's the code I'm using:
-(NSString*)calculateWidthFromHeightString:(NSString*)height usingDimensions:(Favorite*)dimensions{
int decimalPlaces = [self.userData.rounding intValue];
NSUInteger *roundingMethod;
if ([self.userData.roundingMode isEqualToString:#"up"]) {
roundingMethod = NSRoundUp;
}
else if ([self.userData.roundingMode isEqualToString:#"plain"]) {
roundingMethod = NSRoundPlain;
}
else {
roundingMethod = NSRoundDown;
}
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingMethod
scale:decimalPlaces
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
This works as expected, but I'm getting the following compiler warning where I assign the rounding mode to the pointer "roundingMethod":
Incompatible Integer to pointer conversion assigning to ‘NSUInteger *’
(aka ‘unassigned long *) from ‘NSUInteger’ (aka ‘unassigned long’)
Incompatible Integer to pointer conversion assigning to ‘NSUInteger *’
(aka ‘unassigned int *) from ‘NSUInteger’ (aka ‘unassigned int’)
I don't really know what this means. Any help would be greatly appreciated.
This line:
NSUInteger *roundingMethod;
should be:
NSUInteger roundingMethod;
NSUInteger is a native type, not a class type.

BOOL property from a calculation returns NSNumber with incorect value using valueForKey:

I have a simple object which has one NSNumber which is used to store some flags.
I have a conienience getter method which in fact does:
[self.flags integerValue] & SomeConstantFlag
for a property#property (readonly, nonatomic, assign) BOOL someConstantFlag
and this works fine when accesing the underlying bool value like
model.someConstantFlag
but when I try to
id value = [model valueForKey:#"someConstantFlag"];
Then it returns a bad boolean representation e.g. NSNumber with value 2, 4 etc.
Why is this happening when the declaration of the property is BOOL? Is there a "Pretty" way to overcome this issue?
Wrapping on the other hand works ok:
BOOL someBool = 42;
NSNumber* numberVal = #(someBool);
//The underlying is an __NSCFBoolean with the proper 0/1 val!
valueForKey always returns an Objective-C object, even if the property has scalar type.
From the documentation (emphasis mine):
The default implementations of valueForKey: and setValue:forKey:
provide support for automatic object wrapping of the non-object data
types, both scalars and structs.
Once valueForKey: has determined the specific accessor method or
instance variable that is used to supply the value for the specified
key, it examines the return type or the data type. If the value to be
returned is not an object, an NSNumber or NSValue object is created
for that value and returned in its place.
The return value of your method is BOOL, which is defined as
typedef signed char BOOL;
on OS X and on the 32-bit iOS platform. So what valueForKey returns is a NSNumber
containing the result of
signed char val = [self.flags integerValue] & SomeConstantFlag;
and that can be in the range -128 .. 127.
To ensure that you get only YES or NO (aka 1 or 0) write your custom getter as:
-(BOOL)someConstantFlag
{
return ([self.flags integerValue] & SomeConstantFlag) != 0;
}
Remark: On the 64-bit iOS platform (but not on 64-bit OS X), BOOL is defined as the C99 _Bool, which is a "proper" boolean type and can take only the value 0 or 1.
NSNumber *value = #([model objectForKey:#"someConstantFlag"]);
BOOL boolVal = [value boolValue];
I think you should consider the following problems. Firstly, integerValue returns NSInteger which means if you support 64Bit architecture it will return int_64 not int_32, what is more in your code here
[self.flags integerValue] & SomeConstantFlag
this does the following if flags is 00010 and somConstantFlags is 00001 the & of those will do something you probably does not expect because you will get value of 00000 which equals 0 or if they are 00011 and 00110 you will get 00010 which equals 2. So that is why when you call valueForKey you get 2 or 4 or something else depending on your flags :)
What is more in objective-C everything different then 0 is YES.
Try reconsidering your bit logic :). See The following example
enum
{
kWhite = 0,
kBlue = 1 << 0,
kRed = 1 << 1,
kYellow = 1 << 2,
kBrown = 1 << 3,
};
typedef char ColorType;
and in your setter check the following
ColorType pinkColor = kWhite | kRed;
if (pinkColor & (kWhite | kBlue | kRed | kYellow)) {
// any of the flags has been set
}
The flags kWhite, kBlue, kRed and kYellow have been set.
However, kBrown has not been set.

Incompatible pointer to integer conversion 'int' with an expression of type 'id'

In my app I am retrieving data from a MySQL database using JSON.
One of the table fields is idCategoria, from type integer. Now I am trying to assign the idCategoria value to a int variable in my app, but this showing a compiler warning. I have logged the value for categoriaID an it is 143913536 but should be 1.
This is the warning shown:
Incompatible pointer to integer conversion initializing 'int' with an expression of type 'id'
And this is how I am trying to do this:
int categoriaID = [categoriaDescription objectForKey:#"idCategoria"];
Try:
int categoriaID = [[categoriaDescription objectForKey:#"idCategoria"] intValue];

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

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
}

Resources