I’m going through my array and find at which index the searched word is at. I use that information to put space between the words until i reach the next word. I save that text, and then store it in a variable.
I’m suspecting that my comparison between the lastObject and the arr objectAtIndex: i is not working, but i can’t seem to figure out why?
NSArray *arr;
NSScanner *scanner = [NSScanner scannerWithString:_exerciseDocument];
while(![scanner isAtEnd])
{
NSString *buffer;
if([scanner scanCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] intoString:&buffer])
{
[scanner scanUpToString:" " intoString:&buffer];
[scanner scanString:@" " intoString:nil];
}
else
{
}
for(int i=0; i <arr.count; i++)
{
NSString *stringToCheck = (NSString *)[arr objectAtIndex:i];
if([stringToCheck isEqualToString:@"Fokus:"])
{
_descriptionIndex = i;
}
if([stringToCheck isEqualToString:@"Niveau:"])
{
_focusIndex = i;
}
if([stringToCheck isEqualToString:@"Redskab:"])
{
_niveauIndex = i;
}
if([stringToCheck isEqualToString:@"Vanddybde:"])
{
_equipmentIndex = i;
}
}
_descriptiontToTextField = [[NSString alloc]init];
for(int i=1; i <_descriptionIndex; i++)
{
if(![[arr lastObject] isEqual:[arr objectAtIndex:i]])
{
_descriptiontToTextField = [_descriptiontToTextField stringByAppendingString:[ arr objectAtIndex:i]];
_descriptiontToTextField = [_descriptiontToTextField stringByAppendingString:@" "];
}
else
{
_descriptiontToTextField = [_descriptiontToTextField stringByAppendingString:[ arr objectAtIndex:i]];
}
}
First,
-[NSString componentsSeparatedByCharactersInSet:]doesn’t work the way you may think it does. If there are multiple spaces between words in_exerciseDocument, you’re going to end up with a lot of empty strings inarr, which is not what you want. You may want to consider the use of aNSScanner(which, by default, skips whitespace and newline characters) to create your array.Second, arr contains strings, and no other kind of objects. Given this, you should be using
-[NSString isEqualToString:]instead for your comparisons.Third, is there a reason you’re starting your second loop at index 1 instead of index 0?