setIntValue equivalent for NSNumber?

Discussion in 'Mac Programming' started by liptonlover, Sep 4, 2008.

  1. macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #3
    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.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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...
     
  6. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #6
    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
     
  7. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #7
    unless you release returnValue somewhere you've got a memory leak to fix as well. An autoreleased object doesn't need releasing.
     
  8. macrumors newbie

    Joined:
    Jul 4, 2011
    #8
    Learn to read.
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    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.
     

Share This Page