Page 1 of 1

OT: NSComboBox

Posted: Mon Mar 08, 2010 1:54 pm
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?

Posted: Mon Mar 08, 2010 2:01 pm
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.

Posted: Mon Mar 08, 2010 7:26 pm
by JensAyton
You’d have more luck on stackoverflow.

Posted: Thu Mar 11, 2010 8:29 pm
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...

Posted: Thu Mar 11, 2010 9:37 pm
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

Posted: Thu Mar 11, 2010 9:56 pm
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?

Posted: Fri Mar 12, 2010 11:00 am
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... :(

Posted: Sat Mar 13, 2010 3:06 pm
by JensAyton
What index does it log before the exception is thrown?

Posted: Thu Mar 18, 2010 7:25 am
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.

Posted: Fri Mar 19, 2010 7:02 pm
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];