How to pass a integer using performSelectorInBackground - objective-c

I don't want to passAnInt with object. Can I pass it with Int only? if not , how can I do so? Thank you.
[myController performSelectorInBackground:#selector(passAnInt:) withObject:int];

You can box the int within an NSNumber. If your method signature is:
- (void)passAnInt:(NSNumber *)someNumber;
Then you can call it using:
int someInt = 5;
NSNumber *num = [NSNumber numberWithInt:someInt];
[myController performSelectorInBackground:#selector(passAnInt:) withObject:num];
Then, in your method, you can get the int back out again using NSNumber's intValue method. See the NSNumber class reference for more.

Related

Is a variable like "int nameofvariable" an object?

My question is as it is in the title. However, if it isn't an object, how can it then be maintained in NSMutableArray as an object? Because NSMutableArray is only for storing objects or am I wrong? :)
Primitive types, like int, must be wrapped in an object before it can be added to a collection class like NSMutableArray. Try this:
int nameOfVariable = 42;
NSMutableArray *array = ... // the initialized array
[array addObject:#(nameOfVariable)];
The last line is modern syntax that essentially means:
[array addObject:[NSNumber numberWithInt:nameOfVariable]];
Later on, when you need to get the value back, you do:
int someVariable = [array[someIndex] intValue];
No, it is not an object and you can therefore not put it in an NSArray. You have to wrap it in an NSNumber, which is an object and can be put in an NSArray.
An int is a primitive type inherited from C (examples of other primitive types are char, long, float, etc.). These are not Objective-C objects. To add a primitive type to an NSMutableArray or any other Objective-C collection, you must wrap them in an object. For number types like int, you would use NSNumber as follows:
int i = 1;
[array addObject:#(i)]; // #(i) is equivalent to [NSNumber numberWithInt:i]

Using valueForKeyPath on an NSArray, how to count a BOOL property if property is set to YES

I have an NSArray called allQuestions with objects that have a BOOL property called isCorrect
I tried
int res = [self.allQuestions valueForKeyPath:#"#sum.isCorrect"];
but this always gives me a bogus number. I though YES would count as 1, and NO as 0. What am I missing here. Is there no guarantee of the values? If no, what's the best way do achieve this simple thing without iterating over the array?
Edit: just to be precise: I'm interested in the count (number) of elements that evaluate to YES.
int res = [[self.allQuestions valueForKeyPath:#"#sum.isCorrect"] intValue];
valueForKeyPath returns NSNumber's object. You need convert it in to primitive type int or use pointer at NSNumber.
You should wrap the booleans in NSNumber when adding to an array:
BOOL b = YES;
[self.allQuestions addObject:[NSNumber numberWithBool:b]];
And retrieve in the same way:
BOOL b = [[self.allQuestions valueForKeyPath:#"#sum.isCorrect"] boolValue];
Solved:
NSNumber * res = [self.allQuestions valueForKeyPath:#"#sum.isCorrect"];
It returns an NSNumber, of course...

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.

Parameters in output in selector, objective c

i'm executing a method in background, by #selector(method) but i dont' understand how can i return data, for example if method name is:
-(int)methodWithAge:(int)age
i make:
int a = [self performBackgroundThread #selector(methodWithAge:) WithObjects:myAge];
but it doesn't work.
Thanks for any help.
I guess you shuold use NSNumber instead of int
-(int)methodWithAge:(NSNumber*) age
And convert int to NSNumber
NSNumber* num = [NSNumber numberWithInt:age];
int a = [self performBackgroundThread #selector(methodWithAge:) WithObjects:num];
Not using it right. Whats your exact error? I don't know any backin method called performBackgroundThread. You have to pass your values pack to the main thread. Read this tutorial on this topic.

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