I would like to reverse a sentence, but I dont know how I can get the next object correctly in fast enumeration without any problem :/
- (NSString *) reverseWords
{
NSString *result = [[NSString alloc] init];
NSArray *tab_words = [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSEnumerator *reversed = [tab_words reverseObjectEnumerator];
for (NSString *word in reversed)
{
result = [result stringByAppendingString:word];
// Bug here
if ([reversed nextObject])
result = [result stringByAppendingString:@" "];
}
return (result);
}
Thx you
There is an easier way to reverse the sentence than enumerating through it. You can get the reversed array and then join it together with a space (using the
componentsJoinedByString:method) to produce another NSString