Construct NSString from a NSArray

Discussion in 'Mac Programming' started by rbert, Jan 5, 2010.

  1. rbert macrumors newbie

    Joined:
    Nov 15, 2009
    #1
    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!
     
  2. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #2
    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.
     
  3. binkmail macrumors member

    Joined:
    Jan 5, 2010
    #3
    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);
     
  4. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
    #4
    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!
     
  5. God of Biscuits macrumors newbie

    Joined:
    Sep 17, 2007
    #5

    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.
     
  6. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #6
    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.
     
  7. pikester macrumors newbie

    pikester

    Joined:
    Feb 27, 2009
  8. God of Biscuits macrumors newbie

    Joined:
    Sep 17, 2007
    #8
    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.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    It could even be a homework assignment. Best evidence: "Student" is given as an occupation.
     
  10. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #10
    That was my thought when I saw this. Seems an odd exercise for a "real" project...
     

Share This Page