Creating a String with multiple lines

Discussion in 'iOS Programming' started by LT21j, Jun 4, 2013.

  1. LT21j macrumors newbie

    Joined:
    Apr 8, 2013
    #1
    I'm trying to create a string with multiple lines but when i keep appending the string with /n, "/n" actually is printed in the string. And I'm trying to create the string from an array so thats the reason for all the for loops.
    Code:
    -(void)saveSchedule
    {
        NSLog(@"saving schedule");
        NSError *error;
        NSArray *tempArray =_schedule;
        NSString *stringFilepath = @"Schedule.txt";
        NSMutableString *result = [[NSMutableString alloc] initWithCapacity:1000];
        for(NSArray *temp in tempArray)
        {
            int i = 0;
            for (NSObject *obj in temp)
            {
                if (i==3)
                {
                    for(NSString *number in temp[3])
                    {
                        [result appendString:[number description]];
                    }
                        [result appendString:@":"];
                }else
                {
                    [result appendString:[[NSString alloc] initWithFormat:@"%@%@",[obj description], @":"]];
                }
                i++;
            }
            [result appendFormat:@"/n"];
        }
        NSLog(result);
        [result writeToFile:stringFilepath atomically:YES encoding:NSWindowsCP1250StringEncoding error:&error];
    }
    And what is printed in the output is "first row:element:element1:element2:/nsecond row:element:element1:element2:/n". Any ideas on how to fix this?
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    From man printf:

    ;)

    B
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3

    You are using a forward slash and an n. It should be a backslash ("\") and an n.
     
  4. LT21j thread starter macrumors newbie

    Joined:
    Apr 8, 2013
    #4
    Wow I'm an idiot. Thank you.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    No worries. I've done that more than once myself this year, and I've been programming in C off and on since around 1986.
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    Can you mark this thread as resolved?
     

Share This Page