How do I get each line from an NSString?

The following code is straight from Apple's documentation regarding paragraphs and line breaks:

unsigned length = [string length];
unsigned paraStart = 0, paraEnd = 0, contentsEnd = 0;
NSMutableArray *array = [NSMutableArray array];
NSRange currentRange;
while (paraEnd < length)
{
    [string getParagraphStart:&paraStart end:&paraEnd
    contentsEnd:&contentsEnd forRange:NSMakeRange(paraEnd, 0)];
    currentRange = NSMakeRange(paraStart, contentsEnd - paraStart);
    [array addObject:[string substringWithRange:currentRange]];
}

I'm not 100% sure if it will work with 10.4


Enumerates all the lines in a string using enumerateLinesUsingBlock:

[yourString enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
    //line
}];

Declaration

- (void)enumerateLinesUsingBlock:(void (^)(NSString *line,
                                   BOOL *stop))block

Parameters

block The block executed for the enumeration.

The block takes two arguments:

line The current line of the string being enumerated. The line contains just the contents of the line, without the line terminators.

stop A reference to a Boolean value that the block can use to stop the enumeration by setting *stop = YES; it should not touch *stop otherwise.

Availability Available in OS 10.6 and iOS 4.0 and later.


I'll first replace all \r with \n, then replace all \n\n with \n, and then do a componentsSeparatedByString:@"\n".