Monday, May 13, 2013

NSString enumerateLinesUsingBlock example ios

[NSString enumerateLinesUsingBlock]
Enumerates all the lines in a string.
- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block
Parameters of [NSString enumerateLinesUsingBlock]
The block executed for the enumeration.
The block takes two arguments:
The current line of the string being enumerated. The line contains just the contents of the line, without the line terminators.[NSString enumerateLinesUsingBlock]
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.
Example of [NSString enumerateLinesUsingBlock]
__block NSString *firstLine = nil;
NSString *wholeText = [[managedObject valueForKey:@"taskText"] description];
[wholeText enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    firstLine = [[line retain] autorelease];
    *stop = YES;

self.textView.text = firstLine;

Example of [NSString enumerateLinesUsingBlock]
__block NSString * theLine;
[lines enumerateLinesUsingBlock:^(NSString * line, BOOL * stop){
    NSRange range = [line rangeOfString:@"Y"];
    if ( range.location != NSNotFound ) {
        theLine = [line retain];
        *stop = YES;

/* Use `theLine` for something */
[theLine release]; // Relinquish ownership
Example of [NSString enumerateLinesUsingBlock]
NSMutableArray *fileInput = [NSMutableArray array];

[string enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    if ([line length] > 0) {
        [fileInput addObject:
            [line stringByTrimmingCharactersInSet:
                [NSCharacterSet whitespaceCharacterSet]];