TCunningham TCunningham - 1 year ago 93
Objective-C Question

Unfolding Arrays into a Class

I'm working on a project where I read in a comma delimited value file generated by a device. I have no control over the file and it can contain 10-20 named parameters with thousands of values for each parameter over a time period. I plan on associating the DataRecord with the time after. That is, each time will have a DataRecord associated with it. So, my code needs to be dynamic when it comes to generating the parsing data structures. I'm having trouble figuring a way to unwrap the arrays I have parsed from the raw data file. Here is an example.

@interface DataRecord : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *values;

NSArray *nameArray = [[NSArray alloc] initWithObjects:
@"Analog", @"Battery", nil];
NSArray *valueArray1 = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.0f],
[NSNumber numberWithFloat:2.0f],
NSArray *valueArray2 = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.1f],
[NSNumber numberWithFloat:2.1f],
NSArray *valueArray3 = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.2f],
[NSNumber numberWithFloat:2.2f],
NSArray *valueArray4 = [[NSArray alloc] initWithObjects:
[NSNumber numberWithFloat:1.3f],
[NSNumber numberWithFloat:2.3f],
NSArray *arrayOfArrays = [NSArray arrayWithObjects:valueArray1,
valueArray2, valueArray3, valueArray4, nil];

What I'm looking to end up with is an array of Data Records,

name: analog
values: (1.0, 1.1, 1.2, 1.3)

name: battery
values: (2.0, 2.1, 2.2, 2.3)

Maybe I should be using Dictionaries, struct's, I'm not sure, chasing my tail.

Thank you.

Answer Source


NSMutableArray *dataRecords = [NSMutableArray new];
for (NSUInteger i = 0; i < [nameArray count]; i++)
    DataRecord *dr = [DataRecord new]; = nameArray[i];
    NSMutableArray *values = [NSMutableArray new];
    for (NSArray *array in arrayOfArrays)
        [values addObject:array[i]];
    dr.values = values;
    [dataRecords addObject:dr];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download