1

I have got a little problem.

I would like to save an array to a textfile with this code:

for(int aa = 0; aa <= [lines2 count]; aa++) {
    content = [[NSString alloc] initWithFormat:@"%@;%@", content, [lines2 objectAtIndex:aa]];
}

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"cart" ofType:@"txt"]; //meine Zeile
[content writeToFile:filePath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

But it crashes my app. Can anybody give me an easy example, how to convert an NSMutable Array from

One
Two
Three

to an NSString like

One;Two;Three

Hope somebody can help me... :(

2 Answers 2

1

You should be able to do the concatenation using somewhat simpler code as below:

NSMutableString* content = [NSMutableString string];
for (int aa=0; aa < [lines2 count]; aa++){
    [content appendString:[NSString stringWithFormat:@"%@ ",[lines2 objectAtIndex:aa]]];
}

or, You could use componentsJoinedByString.

Sign up to request clarification or add additional context in comments.

1 Comment

I would recommend the componentsJoinedByString approach for clarity and Obj-C idiom.
0

Maybe less, instead of less_or_equal?

for(int aa = 0; aa < [lines2 count]; aa++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.