Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
in the following line,
Code:
[userObject performSelector:userSelector withObject:returnValue];
I need to pass an int. But it only accepts an object. So I decided to use an NSNumber instance. Just before calling the above method, I want to put the value of my int into my NSNumber instance. But looking through the class methods, there is no setInt: method or anything like it. I looked through NSValue as well but nothing there either. How do I do it?
Nate
 
NSNumber is not a mutable class so there are no setXXX methods. Look at the documentation more carefully and you will see both a numberWithInt: convenience constructor and a initWithInt: initialiser suitable for creating a NSNumber with a specific int value.
 
but can I init/create it multiple times, or will I have to destroy it and remake it repeatedly? It's not a static number, it will be changing rapidly over and over again.
 
in the following line,
Code:
[userObject performSelector:userSelector withObject:returnValue];
I need to pass an int. But it only accepts an object. So I decided to use an NSNumber instance. Just before calling the above method, I want to put the value of my int into my NSNumber instance. But looking through the class methods, there is no setInt: method or anything like it. I looked through NSValue as well but nothing there either. How do I do it?
Nate

C'mon! In the last thread I gave you a reasonably good blueprint for passing 4 values via an Object designed just for that purpose. With this method you can only pass two parameters using performSelector:withObject:withObject. Even if you only need 2 now, you'd have to change quite a bit if you needed a third option in the future, whereas if you use an "argument" class, if you need to extend what you are passing you just need to add a field to the class, then set it in your caller and deal with it in your receiver.

https://forums.macrumors.com/threads/555365/

It's worth knowing how to use NSNumber, but I think it would be better to plan for the worst (needing to pass 200 arguments) and have a design that will readily support that, vs. hoping that you never need to improve/extend the functions in question and only allowing up to 2 arguments with your current design.

-Lee
 
but can I init/create it multiple times, or will I have to destroy it and remake it repeatedly? It's not a static number, it will be changing rapidly over and over again.

If you use NSNumber you have to create/destroy it over and over: NSNumber is immutable: it cannot be changed once created. Note that there is no NSMutableNumber class.

If you are really that worried about the overhead of creating lots of NSNumbers instead of re-using one then create your own MyInt class that has a single property intValue that you can change...
 
lee your suggestions were fine, but that's over complicating the situation. I found that I only need to pass one variable. I will certainly keep your post saved on my computer in case I need it, but right now I don't.

Just in case anyone's interested... here's the working code. The animation works almost perfectly, though I have one bug to fix.
Code:
[returnValue release];
returnValue=nil;
returnValue=[[NSNumber alloc] initWithInt:animateInt];
[userObject performSelector:userSelector withObject:returnValue];
Code:
- (void)draw:(NSNumber *)returnValue {
	NSLog(@"draw invoked");
	[field setObjectValue:returnValue];
}

Nate
 
Learn to read.

The release statement is before the alloc/init pair hence the message is being sent to a different object. Therefore caveman_uk is 100% correct. If you are going to resurrect long dead threads to be borderline rude to people please at least try and be correct.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.