iOS Creating a String with multiple lines

LT21j

macrumors newbie
Original poster
Apr 8, 2013
11
0
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?
 

balamw

Moderator
Staff member
Aug 16, 2005
19,360
963
New England
From man printf:

Character escape sequences are in backslash notation as defined in the
ANSI X3.159-1989 (``ANSI C89''), with extensions. The characters and
their meanings are as follows:

\a Write a <bell> character.
\b Write a <backspace> character.
\c Ignore remaining characters in this string.
\f Write a <form-feed> character.
\n Write a <new-line> character.
\r Write a <carriage return> character.
\t Write a <tab> character.
\v Write a <vertical tab> character.
\' Write a <single quote> character.
\\ Write a backslash character.
\num
\0num Write an 8-bit character whose ASCII value is the 1-, 2-,
or 3-digit octal number num.
;)

B
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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?

You are using a forward slash and an n. It should be a backslash ("\") and an n.