ios Block Code
__block NSString * previousString;
__block NSString * currentString;
differentStrings = ^(void){
currentString = [self getString];
NSLog(@"%@", currentString); // not printing anything on console
if (![currentString isEqualToString:previousString])
{
previousString = currentString;
return YES;
}
else
{ return NO;
}
};
differentStrings is a variable pointing to the block.Thus, when you do this:
if (differentStrings)
Remember: differentStrings is a block variable, not a BOOL variable. It contains a block (a function, if you will), which when called will return a bool. Thus, in order to actually run the block, you need to call it. Like this:
differentStrings();
or, in your case:
if (differentStrings())
{
NSLog (@"strings are different");
}
Using a block to find matching First string to previous string
Different Strings is an instance variable. the NSString class also has two methods with block parameters whose names begin with enumerate: enumerateSubstringsInRange:options:usingBlock: and enumerateLinesUsingBlock:. The first method enumerates a string by a text unit of a specified granularity (line, paragraph, word, sentence, and so on); the second method enumerates it by line only.
0 Comment(s)