Anybody know Cocoa?

Posted:
in macOS edited January 2014
I have a button that passes the current selectionIndex of a TableView to my method.



I want the selectionIndex for whatever item is selected in a TableView which is filled with a simple string array.



In the Button's bindings, I bind the argument to the array controller's Controller Key of "selectionIndex", and fill in the "Selector" field with the name of the method.



Don't worry if that is confusing, because it works. The method gets called when the button is clicked.



The method has the form



(void) theMethod: (NSUInteger) theArgument

{

NSLog (@"theArgument=%@\

", theArgument);

}



And this works, printing "theArgument=0" (or 1, or 2, etc) depending on which row is selected when the button is clicked.



Here's the problem: see the object formatter ("%@") in the NSLog? If I make that an integer as it is supposed to be, then I get not the index numbers 0, 1 , etc., but huge numbers, as if they were pointers. The huge numbers show up if



I use the debugger

I use "%d" as the format in NSLog, or

- I execute

Code:




[playerArray removeObjectAtIndex: (NSUInteger) theArgument]; //gets an array index out of bounds error.







But if I use %@ in NSLog, I get the correct numbers.



The result from NSArrayController selectionIndex is supposed to be an NSUInteger, and that is what it is labeled as, but it has the wrong value unless I display it with the "%@" format.



I'm stumped. I have tried taking the intValue of it, tried removing the (NSUInteger) from the method and putting (id) there, etc.



Anybody see why I am getting this integer that is huge and yet correct when displayed as an object ("%@")?

Comments

  • Reply 1 of 6
    Have you tried %u? That's for 32 bit unsigned ints.
  • Reply 2 of 6
    MarvinMarvin Posts: 15,322moderator
    I just looked at some old OBJ-C code and I'm back after throwing up. What I used to do was cast all the values to good old-fashioned C types. So to get a float from an Obj-C object, I'd do:



    float var = (float)[glView getScale:1];



    so maybe:



    (void) theMethod: (NSUInteger) theArgument{

    NSLog (@"theArgument=%d\

    ", (int)theArgument);

    }



    or uint or whatever.



    For strings, I'd always do:



    char* string = (char*)[aFileName cString];



    I was using Obj-C++ though.
  • Reply 3 of 6
    elronelron Posts: 126member
    Quote:
    Originally Posted by lundy View Post


    Here's the problem: see the object formatter ("%@") in the NSLog? If I make that an integer as it is supposed to be, then I get not the index numbers 0, 1 , etc., but huge numbers, as if they were pointers.



    This may be a silly question, but are they pointers? It's been a while since I dabbled with Cocoa (my first thought was that NSUInteger was a subclass of NSNumber, if that tells you how long it's been), so I don't remember exactly how %@ works. I vaguely recall it working on non-object pointers.



    Is it possible that you're not getting a compilation error because the method is not being invoked in the normal way? I don't remember the syntax for it, but you're not getting an ordinary call like this:

    Code:


    [anObject theMethod: 5]





    Instead you're getting this:

    Code:


    SEL s = getSelectorSomehow();

    [anObject invoke: s withArgs: 5];





  • Reply 4 of 6
    mdriftmeyermdriftmeyer Posts: 7,503member
    Reads as a pass by value versus pass by reference misunderstanding of ObjC.
  • Reply 5 of 6
    lundylundy Posts: 4,466member
    Thanks everyone for the suggestions.



    It is a mistake in the documentation for the selectedIndex: method of NSArrayController.



    The docs say that the method returns an NSUInteger, which is a scalar (identical to unsigned int).



    What it actually returns is an NSNumber *, which is an object.That explains why the "%@" correctly unwraps it.



    Coding

    Code:




    [theArgument integerValue]









    Returns the correct scalar integer.
  • Reply 6 of 6
    mdriftmeyermdriftmeyer Posts: 7,503member
    Quote:
    Originally Posted by lundy View Post


    Thanks everyone for the suggestions.



    It is a mistake in the documentation for the selectedIndex: method of NSArrayController.



    The docs say that the method returns an NSUInteger, which is a scalar (identical to unsigned int).



    What it actually returns is an NSNumber *, which is an object.That explains why the "%@" correctly unwraps it.



    Coding

    Code:




    [theArgument integerValue]









    Returns the correct scalar integer.



    hence a pass-by-reference.
Sign In or Register to comment.