views:

349

answers:

2

Hi,

I am relatively new to Objective C and need some array help.

I have a plist with 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 retreive 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 retreive all the array values. Could I use a for loop iterate through the array with all of the values converted to Strings at the output?

Any info would be greatly appreciated.

A: 

try using stringValue...

scoreLabel1.text = [(NSNumber *)[scoresArray objectAtIndex:0] stringValue];
George
Thanks George. This works fine. I wanted to see if I could reduce the amount of lines. Using the above would still require 10 lines.scoreLabel1.text = [(NSNumber *)[scoresArray objectAtIndex:0] stringValue]; scoreLabel2.text = [(NSNumber *)[scoresArray objectAtIndex:1] stringValue]; ect.....
See below for a shorter code snippet.
Alex Reynolds
Oh I see, didn't realize the issue was you needed to repeat the code for the amount of items in the array. Just thought you needed a one liner to set the UILabel text from the array value. Alex's solution is good.
George
+2  A: 
// create NSMutableArray* of score UILabel items, called "scoreLabels"
NSMutableArray *scoreLabels = [NSMutableArray arrayWithCapacity:10];
[scoreLabels addObject:scoreLabel1];
[scoreLabels addObject:scoreLabel2];
// ...

NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
    UILabel *_label = [scoreLabels objectAtIndex:_index];
    _label.text = [NSString stringWithFormat:@"%d", [_number intValue]];
    _index++;
}

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 scoreLabels array, you could indeed retrieve the tag property of the subviews of the view controller (in this case, UILabel instances that you add a tag value to in Interface Builder).

Assuming that the tag value is predictable — e.g., each UILabel from scoreLabel1 through scoreLabel10 is labeled with a tag equal to the values of _index that we use in the for loop (0 through 9) — then you could reference the UILabel directly:

// no need to create the NSMutableArray* scoreLabels here
NSUInteger _index = 0;
for (NSNumber *_number in scoresArray) {
    UILabel *_label = (UILabel *)[self.view viewWithTag:_index];
    _label.text = [NSString stringWithFormat:@"%d", [_number intValue]];
    _index++;
}

The key to making that work is that the tag value has to be unique for the UILabel and must be something you can reference with -viewWithTag:.

The code above very simply assumes that the tag values are the same as the _index values, but that isn't required. (It also assumes the UILabel instances are subviews of the view controller's view property, 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 — UILabel instances get 1000, 1001, and so on, and UIButton instances would get 2000, 2001, etc.

Alex Reynolds
You could even get rid of the scoreLabels array if tags were added to the UILabel objects in Interface Builder and viewWithTag: was used to retrieve them.
gerry3
Thanks Alex. This builds only if I comment out the _index++. Then it returns the final Number in the Array at the label defined at [scoreLabels addObject:scoreLabel1];
Got this working using -viewWithTag, Cheers for the info.