Adding items to a combo box's internal list programmatically
Despite Matt's generous explanation in my last question, I still didn't understand and decided to start a new project and use an internal list.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
codesList = [[NSString alloc] initWithContentsOfFile: @".../.../codelist.txt"];
namesList = [[NSString alloc] initWithContentsOfFile: @".../.../namelist.txt"];
codesListArray = [[NSMutableArray alloc]initWithArray:[codesList componentsSeparatedByString:@"\n"]];
namesListArray = [[NSMutableArray alloc]initWithArray:[namesList componentsSeparatedByString:@"\n"]];
addTheDash = [[NSString alloc]initWithString:@" - "];
flossNames = [[NSMutableArray alloc]init];
[flossNames removeAllObjects];
for (int n=0; n<=[codesListArray count]; n++){
NSMutableString *nameBuilder = [[NSMutableString alloc]init开发者_StackOverflowWithFormat:@"%@", [codesListArray objectAtIndex:n]];
[nameBuilder appendString:addTheDash];
[nameBuilder appendString:[namesListArray objectAtIndex:n]];
[comboBoz addItemWithObjectValue:[NSMutableString stringWithString:nameBuilder]];
[nameBuilder release];
}
}
So this is my latest attempt at this and the list still isn't showing in my combo box. I've tried using the addItemsWithObjectValues
outside the for loop along with the suggestions at this question:
Is this the right way to add items to NSCombobox in Cocoa?
But still no luck. If you can't tell, I'm trying to combine two strings from the files with a hyphen in between them and then put that new string into the combo box. There are over 400 codes and matching names in the two files, so manually putting them in would be a huge chore, not to mention, I don't see what would be causing this problem. The compiler shows no warnings or errors, and in the IB, I have it set to use the internal list, but when I run it, the list is not populated unless I do it manually.
Some things I thought might be causing it:
- Being in the
applicationDidFinishLaunching:
method - Having the string and array variables declared as instance variables in the header (along with @property and @synth done to them)
- Messing around with using appendString multiple times with NSMutableArrays
Nothing seems to be causing this to me, but maybe someone else will know something I don't.
Did you try running this under the debugger and stepping through the code? If you do, I bet you'll find that codesList
, namesList
, or comboBoz
is nil.
By the way, flossNames
isn't doing anything, and the part inside the loop could be done more briefly using -[NSString stringWithFormat:]
.
精彩评论