Method call for every word in NSString

I would like to go through NSStringand call a user-defined function for every word that has a specific criterion (for example, "has 2" L "). I was wondering what is the best way to approach this. Should I use Find / Replace patterns? Blocks?

-(NSString *)convert:(NSString *)wordToConvert{
    /// This I have already written
    Return finalWord;
}

-(NSString *) method:(NSString *) sentenceContainingWords{
    // match every word that meets the criteria (for example the 2Ls) and replace it with what convert: does. 
}
+5
source share
4 answers

The two ways I know for an array loop that will work for you are as follows:

NSArray *words = [sentence componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

for (NSString *word in words)
{
    NSString *transformedWord = [obj method:word];
}

and

NSArray *words = [sentence componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

[words enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id word, NSUInteger idx, BOOL *stop){
    NSString *transformedWord = [obj method:word];
}];

Another method –makeObjectsPerformSelector:withObject:will not work for you. He expects that he will be able to call [word method:obj], which is in the opposite direction from the expected.

+2
source

, -[NSString enumerateSubstringsInRange:options:usingBlock:] NSStringEnumerationByWords NSStringEnumerationLocalized. , . , , , (, "foo, bar" ), , Cocoa.

[aString enumerateSubstringsInRange:NSMakeRange(0, [aString length])
                            options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
    if ([substring rangeOfString:@"ll" options:NSCaseInsensitiveSearch].location != NSNotFound)
        /* do whatever */;
}];

-enumerateSubstringsInRange:options:usingBlock:, , , enclosingRange. , , - [aString replaceCharactersInRange:substringRange withString:replacementString].

+18

, , , , , convert:.

You can also split the string into an array of words using componentsSeparatedByString:or componentsSeparatedByCharactersInSet:, then scroll through the words in the array and determine if they match your criteria. If they fit, translate them to convert:.

Hope this helps.

+1
source

I would recommend using a while loop to go through a line like this.

NSRange spaceRange = [sentenceContainingWords rangeOfString:@" "];
NSRange previousRange = (NSRange){0,0};
do {
   NSString *wordString;
   wordString = [sentenceContainingWord substringWithRange:(NSRange){previousRange.location+1,(spaceRange.location-1)-(previousRange.location+1)}];
   //use the +1 to not include the spaces in the strings
   [self convert:wordString];
   previousRange = spaceRange;
   spaceRange = [sentenceContainingWords rangeOfString:@" "];
} while(spaceRange.location != NSNotFound);

This code will probably need to be rewritten because it is rather rude, but you should get this idea.

Edit: just saw the message of Jacob Gorban, you definitely need to do this.

-1
source

All Articles