0

I have a txt file with some URLs like this

http://url1.com

http://url1.com

http://url1.com

Separated by a line break. How could I add those as different entries separated by line breaks to an NSMutableArray? Thanks :)

4 Answers 4

4

Try something like this:

NSMutableArray *txtLines = [NSMutableArray array];

[txtFile enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    if ([line length] > 0) {
        [txtLines addObject:line];    
    }
}];

Update

@Evan is right, the above only works if blocks are available on your platform. A compiler directive around that code should take care of this limitation, e.g.:

#if NS_BLOCKS_AVAILABLE

// iOS 4.0+ solution

#else

// iOS 2.0+ solution

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

3 Comments

This solution is only available in iOS 4.0 or later.
Oh, this is much more robust that componentsSeparatedByString:. Good call!
@octy: that does not solve the problem, because the code that compiles on 4.0 SDK may run on earlier OS
2
NSString *myListString = /* load / download file */
NSMutableArray *myList = [myListString componentsSeparatedByString:@"\n"];

You may have to use <br/> if it's HTML.

@octy's solution is only available in iOS 4.0 or later. This solution is iOS 2.0 or later. You can check the iOS version and choose which one to use:

BOOL useEnumeratedLineParsing = FALSE;
NSString *reqSysVer = @"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
    useEnumeratedLineParsing = TRUE;

Then check the value of useEnumeratedLineParsing.

5 Comments

This is gonna go wrong, because componentsSeparatedByString is returning a NSArray and not a NSMutableArray. Once you call a NSMutableArray method on it, it's an unrecognized selector and so it will crash.
Not true at all. It will automatically be casted. You may get a warning at compile time, but you can just add the cast.
Tested it and you're right. My bad. To me, it is weird because in the API it says it's a NSArray. Apparently it returns a NSMutableArray. Normally you can't just cast a NSArray to NSMutableArray. (Or am I wrong about that too?)
@BoopMeister: Because NSMutableArray is a subclass of NSArray, a simple cast will work.
@BoopMeister - it is only luck that a NSMutableArray is returned. It may not be the case in future versions. If NSArray was returned, any NSMutableArray methods would indeed cause a crash. It would be safer to do this: myList = [NSMutableArray arrayWithArray: [myListString componentsSeparatedByString: @"\n"]];
2
NSString *textFilePath = [[NSBundle mainBundle] pathForResource:@"urls" ofType:@"txt"];
NSString *fileContentsUrls = [NSString stringWithContentsOfFile:textFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray *myArray = [urls componentsSeparatedByString:@"\n"];

Comments

0

As long as it isn't mega-large, you could read the whole file into an NSString.

NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8Encoding error:nil];

Then split the lines:

NSArray *lines = [text componentsSeparatedByString:@"\n"];

And make it mutable:

NSMutableArray *mutableLines = [lines mutableCopy];

Now, depending on where your text file is coming from, you probably need to be more careful. It could be separated by \r\n instead of just \n, in which case your lines will contain a bunch of extra \r characters. You could clean this up after the fact, using something to remove extra whitespace (your file also might have blank lines which the above will turn into empty strings).

On the other hand, if you're in control of the file, you won't have to worry about that. (But in that case, why not read a plist instead parsing a plain text file...)

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.