initialization makes integer from pointer without a cast - objective-c

here is the code, where listArray is NSMutableArray
......
listArray = [[NSMutableArray alloc] initWithCapacity:100];
for ( int i = 0 ; i < 100 ; i ++ )
[listArray addObject:[NSNumber numberWithInt:i]];
....
for(int max=99; max >= 0 ; max --)
{
int rN = (arc4random() % max) + 0;
//Warning 1: initialization makes integer from pointer without a cast
int temp = [listArray objectAtIndex:rN];
[listArray insertObject:[listArray objectAtIndex:max] atIndex:rN];
//passing argument 1 of 'insertObject:atIndex:' makes pointer from integer without a cast
[listArray insertObject:temp atIndex:max];
}

You need to "unbox" the int value:
int temp = [[listArray objectAtIndex:rN] intValue];
The array stores objects (here an NSNumber), while you want a plain int.
The same holds for insertion, that's why you call [NSNumber numberWithInt:...] while adding.
Edit: The indices in the array range from 0 ... (count-1), not sure if your +1 does what it should.

It should be:
NSNumber *temp = [listArray objectAtIndex:rN];
NSMutableArray holds Obj-C objects, which is why you have to use NSNumber numberWithInt:. That means when you pull it out with objectAtIndex, you get a NSNumber *.

Related

When accessing my NSMutableArray I get an error message like "Cannot initialize a variable of type 'int' with an rvalue of type 'id'"

I have the following function and I am struggling to get the y value out as an int.
+ (NSMutableArray *) cleanPoints:(NSMutableArray *)pointsArray{
NSMutableArray *mutableArray = [NSMutableArray array];
for(int i = 0; i<pointsArray.count-1; i++){
CLog(#"pointsArray %#", pointsArray[i]); /// here it gives me the correct number, like 345
int y = (int)pointsArray[i]; //// here seems to be the problem
CLog(#"y = %d", y); ///// y is a weird number, like y = 384643392
if (y < 4 || y > -4){
y = 0;
}
//create Array
[mutableArray addObject:#(y)];
}
return mutableArray;
}
If I take out the (int) from the int y = (int)pointsArray[I] line then I get the error "Cannot initialize a variable of type 'int' with an rvalue of type 'id'
This line makes no sense:
int y = (int)pointsArray[i];
It is impossible for an NSArray to contain int values. It can contain only objects, and in Objective-C an int is not an object.
Perhaps you mean
int y = [pointsArray[i] intValue];
(but I’m just guessing that this NSArray contains NSNumber objects; I have no way of knowing, as you have given no information about that)

Invalid operations to binary expression ('float' and 'id')

I'm getting this error
Invalid operations to binary expression ('float' and 'id')
NSMutableArray *redValues = [NSMutableArray array];
NSUInteger *redValuesLength = [redValues count];
NSMutableArray *arrayOne = [NSMutableArray array];
NSInteger j;
float diffForAverage, totalOne;
__block int counter = 0;
float average = totalOne / amount;
int amount = 1;
for (j = (counter + 25); j < (redValuesLength - 25); j++)
{
diffForAverage = average - redValues[j + 1]; // error occurs here
if (diffForAverage > -1 && diffForAverage < 1)
{
totalOne += redValues[j + 1]; // error occurs here
amount++;
[arrayOne addObject:[NSNumber numberWithInt:(j - 25)]];
counter++;
}
}
How can I solve this?
Please don't retype error messages (or code) when you post a question. Copy and paste. Otherwise you make mistakes. You can copy Xcode error messages out of the Issue Navigator. Default shortcut: ⌘4.
The actual error message is
Invalid operands to binary expression ('float' and 'id')
The problem is that redValues is an NSMutableArray, so its elements are objects (like NSObject), not primitive numbers (like float and int). The id type is “object identity”, and can refer to any Objective-C object, even those (extremely rare) objects that are not instances of NSObject.
You cannot add a float to an object reference, and you cannot store primitive numbers in an NSMutableArray.
You already know about the NSNumber wrapper object because you create one later in the code. You need to store NSNumber instances in redValues. Perhaps you already are doing that. Then when you get an NSNumber back out of redValues, you need to turn it back into a float to do math on it, like this:
diffForAverage = average - [redValues[j + 1] floatValue]; // error occurs here
If you declare your NSMutableArray like
NSMutableArray<NSNumber *> *redValues = [NSMutableArray array];
then the compiler knows that each element is an NSNumber and you can use the dot syntax (which some people prefer) like
diffForAverage = average - redValues[j + 1].floatValue; // error occurs here

iOS Objective-C - for-loop issue

The code below produces and error in the line [myArray objectAtIndex:i]; and I can't seem to figure out why?
Any ideas?
int total = 0;
for (int i = 0; i < myArray.count; i++) {
int tempNumber = [myArray objectAtIndex:i];
total = total + tempNumber;
}
It could be because you are setting an object to an int. By definition, objectAtIndex returns an object.
Depending on the type of object in myArray, you can try something like this:
int tempNumber = [[myArray objectAtIndex:i] intValue];
If you didn't put ints into your array, then you need to do something extra to get ints out. If you're getting an error with your code above, then it's because you don't have ints in the array, and you need
int myInteger = [[myArray objectAtIndex:i] intValue];
Or something to get the ints out of the array, depending on the rest of your code.

float [] array problem in objective c

I am coding in xcode by objective c. I came up with a problem.
I want to try like
declare float[] weights;
results looks like this
weight[0] = 0.5
weight[1] = 0.4
weight[2] = 0.9
A.h
NSMutableArray *weight;
A.m
weights = [NSMutableArray array];
for(int i=0; i < 4; i++) {
float randomNumber = arc4random() % 11;
[weights addObject:[[NSNumber alloc] initWithFloat:randomNumber]];
NSLog(#" for loops color prob weghts=%f",[weights objectAtIndex:i] );
}
I don't know what is wrong with this code. Please help me to find out?
When I print it by NSLOG " all [weight = 0.000] and also How do I access like [weight floatvalue].
secondly, when I create this class to another class there are weight [0],[1],[2],[3] and no values
It should be [[weights objectAtIndex:i] floatValue] when you print it in your NSLog
NSNumber is a class, not a integral type.
Therefore, you must use %#, not %f.
NSLog(#" for loops color prob weghts=%#",[weights objectAtIndex:i] );
Alternatively, use floatValue, like you said. You may need to cast the object into NSNumber first (for type safety)
NSNumber *number = (NSNumber *)[weights objectAtIndex:i];
NSLog(#" for loops color prob weghts=%f", [number floatValue]);
Also, you are leaking objects here, because you did not release them after putting them in the array. Either release after placing them in array, or use [NSNumber numberWithFloat:]

Simple NSInteger and NSMutableArray question

I'm trying to access an array using another array integer value as an index.
NSInteger index=[appDelegate.randomRiddles objectAtIndex:appDelegate.randomRiddlesCounter];
int i = index;
questionText.text = [[appDelegate.currentRiddlesContent objectAtIndex:i] objectForKey:#"question"];
//where appDelegate.randomRiddlesCounter is an NSInteger and appDelegate.randomRiddles is a NSMutableArray
However I'm getting incompatible pointer to int conversion warning. How can I fix this above code? The warning I get is coming from the first line.
Try:
NSNumber *index = [appDelegate.randomRiddles objectAtIndex: appDelegate.randomRiddlesCounter];
int i = [index intValue];
questionText.text = [[appDelegate.currentRiddlesContent objectAtIndex: i] objectForKey: #"question"];
NSInteger is an integral type, not an object.
Try this:
int i = [index intValue];
An NSArray like object can only store Objective-C object pointers (i.e. everything that you can assign to an id)
With objectAtIndex you get the object, with indexOfObject:(id)anObject you get the corresponding index.
These two instructions are both valid:
id bla = [appDelegate.randomRiddles objectAtIndex:appDelegate.randomRiddlesCounter];
NSInteger index = [appDelegate.randomRiddles indexOfObject:myObject];
The second assumes that myObject is at least of type id
So you try to convert a pointer to an int. Therefore the warning is issued.