PDA

View Full Version : setIntValue equivalent for NSNumber?




liptonlover
Sep 4, 2008, 08:19 AM
in the following line,
[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



robbieduncan
Sep 4, 2008, 08:22 AM
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: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/occ/clm/NSNumber/numberWithInt:) convenience constructor and a initWithInt: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNumber/initWithInt:) initialiser suitable for creating a NSNumber with a specific int value.

liptonlover
Sep 4, 2008, 08:59 AM
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.

lee1210
Sep 4, 2008, 09:02 AM
in the following line,
[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.

http://forums.macrumors.com/showthread.php?t=555365&p=6153874

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

robbieduncan
Sep 4, 2008, 09:04 AM
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...

liptonlover
Sep 4, 2008, 11:47 AM
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.

[returnValue release];
returnValue=nil;
returnValue=[[NSNumber alloc] initWithInt:animateInt];
[userObject performSelector:userSelector withObject:returnValue];


- (void)draw:(NSNumber *)returnValue {
NSLog(@"draw invoked");
[field setObjectValue:returnValue];
}


Nate

caveman_uk
Sep 5, 2008, 03:05 PM
unless you release returnValue somewhere you've got a memory leak to fix as well. An autoreleased object doesn't need releasing.

TheNiggler
Jul 4, 2011, 02:51 AM
unless you release returnValue somewhere you've got a memory leak to fix as well. An autoreleased object doesn't need releasing.



[returnValue release];
returnValue=nil;
returnValue=[[NSNumber alloc] initWithInt:animateInt];
[userObject performSelector:userSelector withObject:returnValue];

Nate

Learn to read.

robbieduncan
Jul 4, 2011, 04:54 AM
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.