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.
Related
Merry Christmas everybody :)
I have a pointer problem. Although I´m familiar with pointer concepts I haven´t used pointers in Objective-C so far the way it´s described here.
I modified it like this:
int countSInteger = 10;
[self setHMSValues:countSInteger];
- (void) setHMSValues: (int*) timeCat {
*timeCat = *timeCat - 1;
}
But now I´receiving a EXC_BAD_ACCESS:
Any Santa hints?
Greetings from Switzerland, Ronald Hofmann
It looks like you want setHMSValues: to calculate and return a value for the integer parameter. However, the parameter is a pointer to an int (int *), and you're passing a plain int with the value of 10. Because pointers are just integer values themselves (with the integer value representing a memory address), the code is trying to set the value at memory location 10; hence, you get a "bad access" error because your program cannot access or change values at memory location 10.
What you should do is pass the address of countSInteger to the method:
[self setHMSValues:&countSInteger];
However, there's a better way to do this. Since you're returning only one value from the method, there's no need for an out parameter. You can change your method to this:
- (int) setHMSValues: (int) timeCat {
return timeCat - 1;
}
and call it like this:
int countSInteger = 10;
countSInteger = [self setHMSValues:countSInteger];
Try:
- (void) setHMSValues: (int*) timeCat {
*timeCat = *timeCat - 1;
}
"countInteger" is declared as an "int *" while the method you're calling into is expecting an "int".
Don't you think you might have better luck if you declare "countInteger" as a plain "int"?
Why use int pointers? Just remove the *
Does Objective-C support pass by reference using & like C++? I haven't checked Objective-C specifically, (never had the need) but in C++ the following is used to tell the compiler to use pass-by-reference:
void count(int &var);
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
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.
I have a UITextField called txtDiscount
It has a value in it: txtDiscount.text == 2.3 //for example
I've tried:
float test = (NSNumber *)txtDiscount.text;
And it compiles, but at runtime breaks down.
Unacceptable type of value for attribute: property = ..."; desired type = NSNumber; given type = NSCFString; value = .
How can I cast the value?
Any help greatly appreciated,
Thanks // :)
You probably want something like:
float test = [txtDiscount.text floatValue];
The NSString documentation provides a list of all the built-in casts.
A cast like this
(NSNumber *)myInstance
is telling the compiler to treat 'myInstance' as if it were an instance of class NSNumber. This may influence compile time warnings and errors. Note: - the compiler. It makes no difference to the code that is generated or run - at all. The code that you are running is still
float test = [txtDiscount text];
where the method -text is returning a pointer to an NSString and you are trying to assign it to a float variable.
see clee's answer for how to get float value from an NSString - but make sure you understand why what you were trying to do is wrong. It will help loads in the long run.
Why doesn't this work?
// spriteArray is an NSMutableArray
int spriteWidth = [spriteArray objectAtIndex:0].contentSize.width;
I get the error:
Request for memeber 'contentSize' in something not a structure or union
If I change the code:
CCSprite *tempSprite = [spriteArray objectAtIndex:0];
int spriteWidth = tempSprite.contentSize.width;
Then it's okay.
I've tried casting:
int spriteWidth = (CCSprite*)[spriteArray objectAtIndex:0].contentSize.width;
But it doesn't work either.
Is there a way to do this without creating the sprite reference?
The return type of objectAtIndex: is id, which is not a struct nor union. If you want to use casting, try
((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width
Otherwise, use a temporary variable.
I believe the . binds tighter than casting. Try
int spriteWidth = ((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width;
See this table.