I am relatively new to Objective C and need some array help.
I have a plist which contains a Dictionary and an NSNumber Array, with more arrays to
be added later on.
NSMutableDictionary *mainArray = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];
NSArray *scoresArray = [mainArray objectForKey:@"scores"];
I need to retrieve all the values from the array and connect them to 10 UILabels which
I’ve set up in interface builder. I’ve done the following to cast the NSNumber to a String.
NSNumber *numberOne = [scoresArray objectAtIndex:0];
NSUInteger intOne = [numberOne intValue];
NSString *stringOne = [NSString stringWithFormat:@"%d",intOne];
scoreLabel1.text = stringOne;
This seems a very long winded approach, I’d have to repeat the 4 lines above ten times to retrieve all the array values. Could I use a for loop to iterate through the array with all of the values converted to Strings at the output?
Any info would be greatly appreciated.
EDIT
I’m not sure why you’d want to comment out
_index++. I haven’t tested this code, so maybe I’m missing something somewhere. But I don’t see anything wrong with_index++— that’s a pretty standard way to increment a counter.As an alternative to creating the
scoreLabelsarray, you could indeed retrieve thetagproperty of the subviews of the view controller (in this case,UILabelinstances that you add atagvalue to in Interface Builder).Assuming that the
tagvalue is predictable — e.g., eachUILabelfromscoreLabel1throughscoreLabel10is labeled with atagequal to the values of_indexthat we use in theforloop (0 through 9) — then you could reference theUILabeldirectly:The key to making that work is that the
tagvalue has to be unique for theUILabeland must be something you can reference with-viewWithTag:.The code above very simply assumes that the
tagvalues are the same as the_indexvalues, but that isn’t required. (It also assumes theUILabelinstances are subviews of the view controller’sviewproperty, which will depend on how you set up your interface in Interface Builder.)Some people write functions that add 1000 or some other integer that allows you group types of subviews together —
UILabelinstances get 1000, 1001, and so on, andUIButtoninstances would get 2000, 2001, etc.