ARC issue with devowelizer


#1

For some reason I’m getting an error… ARC issue… no visual @interface for NSMutableString declares the selector… etc. etc. etc.

I’ve gotten this error on a previous project in this book, and just gave up… is there something I’m missing here?

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    //Create an array of strings to devowelize and a container for new ones
    NSArray *oldStrings = [NSArray arrayWithObjects: 
                           @"Sauerkraut", @"Raygun", @"Big Nerd Ranch", @"Mississippi", nil];
    NSLog(@"oldStrings: %@", 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 blocks variable
// void (^devowelizer)(id, NSUInteger, BOOL *);
//
// //Assign a block to the variable
// devowelizer = ^(id string, NSUInteger i, BOOL *stop)

    //You can declare & assign in the same line... as with all variables
    void (^devowelizer)(id, NSUInteger, BOOL *)= ^(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 replaceOccurencesOfStrings:s 
                                       withString:@""
                                          options:NSCaseInsensitiveSearch
                                            range:fullRange];
        }
        
        [newStrings addObject:newString];
        
    }; //End of block assignment
}
return 0;

}
[/code]

The error occurs on this line

[newString replaceOccurencesOfStrings:s withString:@"" options:NSCaseInsensitiveSearch range:fullRange];

Thanks, and I hope this isn’t too stupid of a question. I know it’s something simple that I’m just not getting.


#2

Your method name is not quite right.

[color=#FF0000]replaceOccurencesOfStrings[/color]:…

Use the correct method name.

The following extract is from Apple’s class reference for NSMutableString:

replaceOccurrencesOfString:withString:options:range:

Replaces all occurrences of a given string in a given range with another given string, returning the number of replacements.

- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange

To avoid this kind of errors, either use Xcode’s auto-completion feature or copy and paste the method names from the class reference.

(Displaying the class reference is a breeze in Xcode: option-click the name of a class; for example: option-click NSMutableString.)


#3

Can’t believe I missed that… For some reason I was thinking that error indicated something other than a syntax problem.

Thanks a lot. Really appreciate it.