Anybody know Cocoa?
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
[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 ("%@")?
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
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.
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:
[anObject theMethod: 5]
Instead you're getting this:
SEL s = getSelectorSomehow();
[anObject invoke: s withArgs: 5];
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
[theArgument integerValue]
Returns the correct scalar integer.
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
[theArgument integerValue]
Returns the correct scalar integer.
hence a pass-by-reference.