出于某种原因,我无法将第二条NSLog消息输出到控制台。已经找到了问题,但找不到它。提前致谢。Second NSLog message will not print
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// Create the array of strings to devowelize and a container for new ones
NSArray *oldStrings = [NSArray arrayWithObjects:@"Sauerkraut", @"Raygun", @"Big Nerd Ranch", @"Mississippi", nil];
NSLog(@"old strings: %@", oldStrings);
NSMutableArray *newStrings = [NSMutableArray array];
// Create a list of characters that we'll remove from the string
NSArray *vowels = [NSArray arrayWithObjects:@"a", @"e", @"i", @"o", @"u", nil];
// Declare the block variable
void(^devowelizer)(id, NSUInteger, BOOL *);
// Assign a block to the variable
devowelizer = ^(id string, NSUInteger i, BOOL *stop)
{
NSMutableString *newString = [NSMutableString stringWithString:string];
// Iterate over the array of vowels, replacing occurences of each with
// an empty string
for (NSString *s in vowels) {
NSRange fullRange = NSMakeRange(0, [newString length]);
[newString replaceOccurrencesOfString:s
withString:@""
options:NSCaseInsensitiveSearch
range:fullRange];
[newStrings addObject:newString];
}; //End of block assignment
// Iterate over the array with our block
[oldStrings enumerateObjectsUsingBlock:devowelizer];
NSLog(@"new strings: %@", newStrings);
};
return 0;
}
}
这是正确答案,块'devolwelizer'永远不会调用。你只是定义了块,就是这样,块本身不能被执行。 – holex 2012-07-14 17:37:29
不明白;是不是我传递的块与行:[oldStrings enumerateObjectsUsingBlock:devowelizer]; ? – pdenlinger 2012-07-15 06:41:56
该行传入该块,但该行只有在该块执行时才会执行。把它看作写一个函数,然后在其内部调用该函数。如果函数外部的某个函数调用该函数,该函数将首先被调用。 – matzahboy 2012-07-15 18:47:02