出於某種原因,我無法將第二條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