Objective-C can't convert NSNumber to int - objective-c

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

Related

Bad receiver type 'NSUInteger *' (aka 'unsigned int *')

Here is my method createNewRectangleWithHeight and the parameter is heightParam and widthParam. My problem is i can't use the parameter in the method.
I am getting an error something like this
Bad receiver type 'NSUInteger *' (aka 'unsigned int *')
-(BOOL)createNewRectangleWithHeight:(NSUInteger *)heightParam width:(NSUInteger *)widthParam{
if ([[heightParam length] == 0] || [widthParam length]==0]) {
NSLog(#"The height and width must no be 0");
}
}
Error is in if condition
You can only call methods on objects. A pointer to an unsigned int is not an object; it's just the address of a number.
You don't need to pass addresses unless you're changing the value inside your method and you can just check the value rather than treating the number as an object.
-(BOOL)createNewRectangleWithHeight:(NSUInteger)heightParam width:(NSUInteger)widthParam {
if (heightParam == 0 || widthParam == 0) {
NSLog(#"The height and width must not be 0");
}
}
Learn to look at the error messages you get. In this case the error message is telling you exactly what's wrong. The syntax [object message] is sending a message to an object. NSInteger is a scalar type, not an object type.
BTW, your method does not return a result, and is badly named. It should be called something like heightAndWidthAreNotZero.
Others have already pointed out that you should be using (NSUInteger), not (NSUInteger *), as your parameter types. Actually, if you're getting ready to create a CGRect, you should probably be using CGFloat, not NSUInteger, since the different values of a CGRect are CGFloat type.
Finally, there is a built-in system function CGRectIsEmpty() that takes a CGRect as input and returns TRUE if the rectangle is empty, and FALSE if it's not empty.

Invalid Operands to Binary Expression (Int and NSNumber *)

I'm learning objective C from CodeSchool and there's one section of learning OOP which I don't understand why it won't work but seems so simple to fix.
The Code:
- (void) decreaseBatteryLife:(NSNumber *)decreaseBy
{
self.batteryLife = #([self.batteryLife intValue] - decreaseBy);
}
The error which points to the minus symbol before decreaseBy:
^invalid operands to binary expression ('int' and 'NSNumber *')
"decreaseBy" is a NSNumber object while the other value is a "int". There's a difference (the first is an Objective C object, the other is a raw C type).
You need to get the raw "intValue" of your "decreaseBy" number.
Something like this:
self.batteryLife = #([self.batteryLife intValue] - [decreaseBy intValue]);

Comparing integers in an array: (Invalid operands to binary expression ('NSUInteger *' (aka 'unsigned int *') and 'NSUInteger *')

I am attempting to take an array called _operationArray that has NSNumbers stored within it. I want to take the last two elements of the array and add them together.
In the following code, endObject should be the last element space in the array,
secondToEndObject should be the second to last element space. I then attempt to
int singleSpace = 1;
int doubleSpace = 2;
NSUInteger endObject = [_operationArray count] - singleSpace;
NSUInteger secondToEndObject = [_operationArray count] - doubleSpace;
NSUInteger *firstNumber =[[_operationArray objectAtIndex:endObject] integerValue] ;
NSUInteger *secondNumber = [[_operationArray objectAtIndex:secondToEndObject]
integerValue];
/* These two lines defining first and second Number both have warnings saying: Incompatible integer to pointer conversion initializing 'NSUInteger *' (aka 'unsigned int *') with an expression of type 'NSInteger' (aka 'int')*/
_theResult = firstNumber + secondNumber;
/* This last line has a error saying: Invalid operands to binary expression ('NSUInteger *' (aka 'unsigned int *') and 'NSUInteger *')*/
I'm incredibly new to Objective-C and Xcode, so I don't even understand what these errors actually mean. Any help would be appreciated.
I guess you shoudl get rid of the asterisk.
NSUInteger firstNumber =[[_operationArray objectAtIndex:endObject] integerValue] ;
NSUInteger secondNumber = [[_operationArray objectAtIndex:secondToEndObject]
integerValue];
integerValue returns an NSUInteger (aka unsigned int) and not a pointer to it.
BTW, it returns an NSInteger. Meaning you may run into issues if the value returned is negative.

Implicit conversion of NSInteger error

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

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.