Implicit conversion of NSInteger error - objective-c

Using MagicalRecord, I am trying to get the record with a particular clientNumber which is a NSInteger (defined as int16 as the data type).
This is my line of code where I'm getting the error:
ClientInfo *clientSelected = [ClientInfo MR_findFirstByAttribute:#"aClientNumber" withValue: clientNumber inContext:localContext];
UPDATE: This is the definition of MR_findFirstByAtytribute:
MR_findFirstByAttribute:(NSString *) withValue:(id)
This is the error I'm getting:
Implicit conversion of NSInteger (aka int) is disallowed with ARC
For the life of me, I don't see what's wrong. ClientInfo is defined as
#interface ClientInfo : NSManagedObject

The parameter type for withValue is an id (a pointer). NSInteger is a scalar value (not an object) and cannot be converted to a pointer value implicitly.
This is purely a guess, but creating an NSNumber from the NSInteger might work:
NSNumber *val = [NSNumber numberWithInteger:clientNumber];
ClientInfo *clientSelected = [ClientInfo MR_findFirstByAttribute:#"aClientNumber" withValue:val inContext:localContext];

As per your definition of method it's expecting and id and your are passing it a NSInteger aka int

Related

Objective-C can't convert NSNumber to int

I have a simple code that will iterate through an array of integers but when I tried using a "for in" loop, it said I needed to have an object so I used an NSNumber but in order for my code to work, it had to be an int so after the loop, I had it convert the NSNumber to an int. It gave me another error saying I need to declare the variable _strong for it to work so I did that but now it gives me these errors: "Incompatible pointer to integer conversion sending 'NSNumber *_strong' to parameter of type 'int'" and "Implicit conversion of 'int' to 'NSNumber *' is disallowed with ARC" Here is the code that gives an error:
for(__strong NSNumber *i in factors) {
i = [i intValue];
You've already declared "i" as an Objective C object (specifically a NSNumber object).
If you want to do the conversion, declare a separate and different variable, e.g.
int j;
and set your integer to that, like:
j = [i intValue];

KVC - about data type

this is the code:
NSNumber *taskId = [[self.taskList objectAtIndex:indexPath.row] valueForKey:#"identity"];
NSInteger *intTaskId = [[self.taskList objectAtIndex:indexPath.row] valueForKey:#"identity"];
self.taskList is an NSArray which filled with core data fetch request in ViewController's viewDidLoad method.
the taskId is: 1
the intTaskId is: 269303816
In actually, the value stored in core data is: 1
below is my questions:
1, I am confused why the NSInteger incorrect?
2, Should I have to replace NSInteger with NSNumber to avoid any other problems?
NSNumber is an object, whereas NSInteger is simply a typedef for a primitive (non-object) type (like int). NSInteger is not a subclass of NSNumber. Core Data returns numbers as instances of NSNumber. You're getting the weird NSInteger value because it's pointing to an object of type NSNumber but attempting to print it as if it were just an integer.
You'll need to replace NSInteger with NSNumber to avoid any problems. You could also use the intValue method on NSNumber to get back an NSInteger:
NSNumber *objTaskId = [[self.taskList objectAtIndex:indexPath.row] valueForKey:#"identity"];
NSInteger *intTaskId = [objTaskId intValue];
You'll need to do this if you want to do comparisons (greater than, equal too, smaller than) or arithmetic (you can't add an NSNumber to another NSNumber or an NSNumber to a primitive type like an int or float).

Why am I getting an integer to pointer conversion error in objective-c?

I am looping through an NSString object called previouslyDefinedNSString and verifying if the integer representing the ASCII value of a letter is in an NSMutableSet called mySetOfLettersASCIIValues, which I had previously populated with NSIntegers:
NSInteger ASCIIValueOfLetter;
for (int i; i < [previouslyDefinedNSString length]; i++) {
ASCIIValueOfLetter = [previouslyDefinedNSString characterAtIndex:i];
// if character ASCII value is in set, perform some more actions...
if ([mySetOfLettersASCIIValues member: ASCIIValueOfLetter])
However, I am getting this error within the condition of the IF statement.
Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int') to parameter of type 'id';
Implicit conversion of 'NSInteger' (aka 'int') to 'id' is disallowed with ARC
What do these errors mean? How am I converting to an object type (which id represents, right?)? Isn't NSInteger an object?
You want to make it an NSNumber, as in:
NSInteger ASCIIValueOfLetter;
for (int i; i < [previouslyDefinedNSString length]; i++) {
ASCIIValueOfLetter = [previouslyDefinedNSString characterAtIndex:i];
// if character ASCII value is in set, perform some more actions...
if ([mySetOfLettersASCIIValues member: [NSNumber numberWithInteger: ASCIIValueOfLetter]])
Now you're going to have the result you're looking for.
These errors mean that member: expects an object. id is a pointer to an Objective-C object, and instead of an object, you're passing in a primitive type, or scalar (despite its NS- prefix, NSInteger is not an object - just a typedef to a primitive value, and in your case, an int). What you need to do is wrap that scalar value in an object, and specifically, NSNumber, which is a class specifically designed to handle this.
Instead of calling member: with ASCIIValueOfLetter, you need to call it with the wrapped value, [NSNumber numberWithInteger:ASCIIValueOfLetter], as MaurĂ­cio mentioned.

Is it possible to cast an NSInteger to NSNumber?

Is it possible to cast a NSInteger to a NSNumber object?
I need to convert the tag of a UIImageView object to a NSNumber object because I need to pass it as an argument to a function.
You cannot cast it because NSInteger is not an object, just an alias for a built-in type. You can always create a new NSNumber object from NSInteger, like this:
NSNumber *myNum = #(myNsIntValue);
or in the prior version of the compiler, use
NSNumber *myNum = [NSNumber numberWithInteger:myNsIntValue];
since Apple LLVM Compiler 4.0, there is an easier way to create NSNumber object:
NSNumber *x = #1234;
NSNumber *y = #(anIntegerVariable);
This is the more correct answer and it will not produce unexpected error.
NSNumber *myNum = [NSNumber numberWithInteger:myNsIntValue];
Because the doc said:
"numberWithInteger:
Creates and returns an NSNumber object containing a given value, treating it as an NSInteger."
"numberWithInt:
Creates and returns an NSNumber object containing a given value, treating it as a signed int."

passing argument ... makes integer from pointer without a cast

How can I solve this problem?
code:
[NSNumber numberWithInteger:[buttonStatsInSection objectAtIndex:row]]
warning: passing argument 1 of 'numberWithInteger:' makes integer from pointer without a cast
Thanks.
numberWithInteger: needs you to give it an int to create the NSNumber. You are giving it an object, because objectAtIndex: returns an object.
Even if the object you have at that row is an NSNumber, or anything else, you still need to get an actual int data type out of it somehow.
For example, if the object you get back is an NSNumber, you could have something like this in the end:
NSNumber * myNSNum = [buttonStatsInSection objectAtIndex:row];
int myInt = [myNSNum intValue];
[NSNumber numberWithInteger:myInt];
Why are you creating a new NSNumber object? Do you really want a copy of it, or do you just want a reference to it? What do you plan to do with it? If you want a copy, you can just do:
NSNumber* copy = (NSNumber*)[[buttonStatsInSection objextAtIndex:row] copy];
Otherwise just do:
NSNumber* num = (NSNumber*)[buttonStatsInSection objextAtIndex:row];
Either way it isn't necessary to go through the process of extracting the int value and the converting it right back to an NSNumber