Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
hello,

I have a array:
NSArray *myArray = [NSArray arrayWithObjects:mad:"Red", @"John", @"20", @"Student", @"Blue", @"Jane", @"24", @"Singer", @"Green", @"Kath", @"25", @"Teacher", nil];

I would like to product a result as NSString like this:
1 | Color: Red | Name: John | Age: 20 | Occupation: Student
2 | Color: Blue | Name: Jane | Age: 24 | Occupation: Singer
3 | Color: Green | Name: Kath | Age: 35 | Occupation: Teacher

I tried with the for loop, but could not get the result like this. Can you please help me.
Thank you!
 
How about something like this (untested & may contain spelling mistakes...)

Code:
NSMutableString *string = [NSMutableString string];

for (NSUInteger i = 0; i < ([myArray count] / 4); i++) {
    NSUInteger offset = (i * 4);
    [string appendFormat:@"%i ¦ Color: %@ ¦ Name: %@ ¦ Age: %@ ¦ Occupation: %@ \n", 
        i + 1,
        [myArray objectAtIndex:offset], 
        [myArray objectAtIndex:offset+1], 
        [myArray objectAtIndex:offset+2], 
        [myArray objectAtIndex:offset+3] ]
}

Which goes through the array in blocks of 4 and appends the information for the next person at the end of a string.
 
Something like this: (not tested)

Code:
- (void)myMethod {
NSString * myFormat = @"%d | Color: %@ | Name: %@ | Age: %@ | Occupation: %@\n";
NSString * myResult = @"";
NSArray *myArray = [NSArray arrayWithObjects:@"Red", @"John", @"20", @"Student", @"Blue", @"Jane", @"24", @"Singer", @"Green", @"Kath", @"25", @"Teacher", nil]; 

int colsInArray = 4; // red, john, 20, student
int rowsInArray = [myArray count] / colsInArray; // should result in 3

int position = 0;

while( position < rowsInArray ) {
    myResult = [myResult stringByAppendingFormat:myFormat, [myArray objectAtIndex:(position*colsInArray)], 
               [myArray objectAtIndex:(position*colsInArray)+1], 
               [myArray objectAtIndex:(position*colsInArray)+2], 
               [myArray objectAtIndex:(position*colsInArray)+3]];

    position++;
}

NSLog(myResult);
 
Thank you JoshDC and binkmail!
I sit hours to solve this complexe struction without success. Your both solutions are working well and they are worth for my example collection archive.
Thank you again for your help!
 
Thank you JoshDC and binkmail!
I sit hours to solve this complexe struction without success. Your both solutions are working well and they are worth for my example collection archive.
Thank you again for your help!


Have you considered that storing your data in an array isn't the right way to go?

turning the array into a list of strings is not the way to archive the data efficiently.

What you really want to do here is end up with an array of dictionaries, where each dictionary has 4 key value pairs:

keyColor / value
keyName / value
keyAge / value
keyOccupation / value

Once you have an array of these dictionaries you can write it out to a file using a built-in Cocoa function and read it back into memory just as easily without doing any of this string assembly stuff.
 
Have you considered that storing your data in an array isn't the right way to go?

turning the array into a list of strings is not the way to archive the data efficiently.

What you really want to do here is end up with an array of dictionaries, where each dictionary has 4 key value pairs:

keyColor / value
keyName / value
keyAge / value
keyOccupation / value

Once you have an array of these dictionaries you can write it out to a file using a built-in Cocoa function and read it back into memory just as easily without doing any of this string assembly stuff.

Better still, use Core Data which this seems to be the perfect situation for. In the original posters defence, I don't see how an array of dictionaries is anything other than less efficient provided the array is properly constructed which shouldn't be very hard to achieve.
 
Better still, use Core Data which this seems to be the perfect situation for. In the original posters defence, I don't see how an array of dictionaries is anything other than less efficient provided the array is properly constructed which shouldn't be very hard to achieve.

It's better for the same reason that you suggest Core Data: structured data is best stored in a way which captures not only the data but also the structure of the data.

Storing it in a lossy way and having to maintain the structure of the data within the source code violates MVC by putting model (structure of the data) in the controller (source code which serializes/deserializes and otherwise manipulates and maintains the structure of the data).

The only reason you might want to stay away from Core Data is that it might be overkill. Reading and writing XML .plist files and using NSMutableDictionary objects may suit just fine here.

The original poster has given no indication of the size/scope of the application.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.