OT: NSComboBox

Off topic discussion zone.

Moderators: winston, another_commander, Cody

Post Reply
User avatar
harry747
Deadly
Deadly
Posts: 135
Joined: Mon Feb 23, 2009 8:57 pm
Location: England

OT: NSComboBox

Post by harry747 »

I know this is off-topic, but I'm hoping that one of you guys can point me in the right direction
anyway, I have a window with 3 NSComboBoxes. 2 of them work ok. But the third one uses a data source, and when I try to use it, I keep getting SIGABRT and EXC_BAD_ACCESS. what am I doing wrong?
MacBook pro, OSX 10.8.5, Xcode 5
another_commander
Quite Grand Sub-Admiral
Quite Grand Sub-Admiral
Posts: 6683
Joined: Wed Feb 28, 2007 7:54 am

Post by another_commander »

Not Oolite related, moved to Outworld.

Some more information will be needed, I believe. In cases like this, posting the code that crashes can result in more accurate responses and faster solutions. My guess would be a wild pointer which you think it refers to the source data, when it may be nil or invalid.
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

You’d have more luck on stackoverflow.
User avatar
harry747
Deadly
Deadly
Posts: 135
Joined: Mon Feb 23, 2009 8:57 pm
Location: England

Post by harry747 »

another_commander wrote:
Not Oolite related, moved to Outworld.

Some more information will be needed, I believe. In cases like this, posting the code that crashes can result in more accurate responses and faster solutions. My guess would be a wild pointer which you think it refers to the source data, when it may be nil or invalid.
i assume you mean the delegate methods. here they are.

Code: Select all

-(NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
	NSUInteger number = [symbols count];
	return number;
}

-(id)comboBox:(NSComboBox *)sender objectValueForItemAtIndex:(NSInteger)index {
	NSLog(@"%i", index);
	NSString *response = [symbols objectAtIndex:(int)index];
	return response;
}
symbols is an NSMutableArray which contains 5 NSStrings.

well, at least i've found out why i get an exception: the index is out of bounds of the array. but the question is, WHY is the index out of bounds, and what can i do about it? i've tried out typecasts, but so far no luck. gonna keep trying...
MacBook pro, OSX 10.8.5, Xcode 5
User avatar
Frame
---- E L I T E ----
---- E L I T E ----
Posts: 1477
Joined: Fri Mar 30, 2007 8:32 am
Location: Witchspace

Post by Frame »

harry747 wrote:
another_commander wrote:
Not Oolite related, moved to Outworld.

Some more information will be needed, I believe. In cases like this, posting the code that crashes can result in more accurate responses and faster solutions. My guess would be a wild pointer which you think it refers to the source data, when it may be nil or invalid.
i assume you mean the delegate methods. here they are.

Code: Select all

-(NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
	NSUInteger number = [symbols count];
	return number;
}

-(id)comboBox:(NSComboBox *)sender objectValueForItemAtIndex:(NSInteger)index {
	NSLog(@"%i", index);
	NSString *response = [symbols objectAtIndex:(int)index];
	return response;
}
symbols is an NSMutableArray which contains 5 NSStrings.

well, at least i've found out why i get an exception: the index is out of bounds of the array. but the question is, WHY is the index out of bounds, and what can i do about it? i've tried out typecasts, but so far no luck. gonna keep trying...
While my area is more c++, this looks like the classic

"forgot to count in element zero"

so your first method tells you that there are 10 symbols so the number it returns is 10

however, element symbols [10] does not exist, and you never use symbols[0] since the array is from 0 to 9 = 10 elements

quick solution: without testing

Code: Select all

-(NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
	NSUInteger number = [symbols count];
	return number-1;
}

-(id)comboBox:(NSComboBox *)sender objectValueForItemAtIndex:(NSInteger)index {
	NSLog(@"%i", index);
	NSString *response = [symbols objectAtIndex:(int)index];
	return response;
}
notice the subtract 1 in the first method

add your own code to prevent number from going "negative"

This is classic btw.. ;-)
Cheers Frame
Bounty Scanner
Number 935
User avatar
harry747
Deadly
Deadly
Posts: 135
Joined: Mon Feb 23, 2009 8:57 pm
Location: England

Post by harry747 »

While my area is more c++, this looks like the classic

"forgot to count in element zero"

so your first method tells you that there are 10 symbols so the number it returns is 10

however, element symbols [10] does not exist, and you never use symbols[0] since the array is from 0 to 9 = 10 elements
i know all that, but it doesn't apply here. those methods are not called by my code, they are called by the combobox. it's how it knows what items to display when the user selects it.

besides, if that "element zero" stuff was the problem, it should work the first few times and then throw an exception. but instead i get the exception the first time, every time.

PS. did i mention this topic is for mac developers?
MacBook pro, OSX 10.8.5, Xcode 5
User avatar
Frame
---- E L I T E ----
---- E L I T E ----
Posts: 1477
Joined: Fri Mar 30, 2007 8:32 am
Location: Witchspace

Post by Frame »

harry747 wrote:
i know all that, but it doesn't apply here. those methods are not called by my code, they are called by the combobox. it's how it knows what items to display when the user selects it.

besides, if that "element zero" stuff was the problem, it should work the first few times and then throw an exception. but instead i get the exception the first time, every time.

PS. did i mention this topic is for mac developers?
Well English is not my primary language, and yes I'm on windows and no you are not exactly coming of friendly like... :(
Bounty Scanner
Number 935
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

What index does it log before the exception is thrown?
User avatar
harry747
Deadly
Deadly
Posts: 135
Joined: Mon Feb 23, 2009 8:57 pm
Location: England

Post by harry747 »

Ahruman wrote:
What index does it log before the exception is thrown?
the exception says the index is -1 which is obviously out of bounds. i've double-checked, i'm using the types that the documentation says i should use which is why i don't understand why the index is -1.
MacBook pro, OSX 10.8.5, Xcode 5
User avatar
JensAyton
Grand Admiral Emeritus
Grand Admiral Emeritus
Posts: 6657
Joined: Sat Apr 02, 2005 2:43 pm
Location: Sweden
Contact:

Post by JensAyton »

I would expect -1 represents some special value, like a placeholder, but I can’t find any relevant documentation and it doesn’t happen in a test app I threw together.

I suggest adding the following at the top of your -comboBox:objectValueForItemAtIndex: and seeing if it shows up:

Code: Select all

if (index < 0) return [NSString stringWithFormat:@"<< NEGATIVE: %li>>", index];
Post Reply