Q:

iOS: How to remove invalid characters from a search string, while allowing the search on multiple words

This is a follow-up question to this one..
Thanks to Nick Lockwood, I can use this code to filter bad characters:
NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];

However, I have strings like "Me, You" or "This isn't the one".
I'd like to be able to search for "You" or "the one".
The code above seems to limit me too much, and I can only search for words up to the special character.. 
Could I have some more help please?
UPDATE:
I've tried using this code:
NSString *badChars = @"\\\'";
NSCharacterSet *badCharSet = [NSCharacterSet characterSetWithCharactersInString: badChars];
NSString *safeSearchString = [sbar.text stringByTrimmingCharactersInSet:badCharSet];

but no joy.. Please forgive all the newbie questions...
SECOND UPDATE:
I've tried this code too. It's a delegate method:
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ((text == @"\\") || (text == @"\'")) {
        return NO;      
    } else {
        return YES;
    }
}

It almost works but doesn't survive a predicate check: "Unable to parse the format string "main contains[cd] 'isn't' OR interpret contains[cd] 'isn't' OR summary contains[cd] 'isn't' OR data contains[cd] 'isn't'"'...

A:

I'm not sure you understand what the code in your example is doing.  What that code does is:

create a set of all alphanumeric characters
create an inverse of that set (i.e. all NON-alphanumeric characters)
break your original string into an array of substrings, separated by any characters in the non-alphanumeric char set
reassemble the substrings in that array back into one string as your final result

The final result is your original string minus any non-alphanumeric characters.  So if your original searchString was this:
This isn't the one

Then your safeSearchString will be:
Thisisnttheone

I think this is probably not what you want.  I don't know exactly the context of what you are doing, but I can give some ideas that might help:
If you want to only remove SOME illegal characters from your string instead of all non-alphanumeric, you could create a custom character set like this:
// remove only apostrophe and dollar sign characters
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"'$"];
NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];

If you want to remove all non-alphanumeric characters but keep the spaces:
// remove any characters except alphanumeric and space
NSMutableCharacterSet *charSet = [NSMutableCharacterSet characterSetWithCharactersInString@" "];
[charset formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];

You might also want to remove non-alphanumeric/non-space characters and then separate into an array of individual words:
NSString * searchString = @"This isn't the one";  // example

// remove any characters except alphanumeric and space and break into an array on word boundaries
NSMutableCharacterSet *charSet = [NSMutableCharacterSet characterSetWithCharactersInString@" "];
[charset formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
NSArray *safeSearchWords = [safeSearchString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSString *word;
for (word in safeSearchWords) {
    NSLog(@"%@",word);
}

// This
// isnt
// the
// one

