开发者

Objective-c: Comparing two strings not working correctly

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    if(tempArray != nil){
        for (int i = 0; i < [tempArray count]; i++)
        {
            if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]])
        //  if([sectionInfo indexTitle] == [tempArray objectAtIndex:i])
            {
                NSLog(@"f开发者_如何学编程ound");
                break;
            } else
                {
                    NSLog(@"Not found %@", [sectionInfo indexTitle]);
                    [tempArray addObject:[sectionInfo indexTitle]];
                    NSLog(@"array %@", tempArray);
                    return [tempArray objectAtIndex:i];
                }
        }
    } 
}

The comparison of the strings in the if statement never resolves to true. The sample data has two instances of duplicates for testing purposes. The commented line is an alternate, although I believe incorrect, attempt to compare the section with the string in the tempArray.

What am I doing incorrectly?

Also, all data is in capital letters, so the comparison is not a matter of lower to upper case.

EDIT:

The NSLogs:

                NSLog(@"Not found %@", [sectionInfo indexTitle]);
                NSLog(@"section: %d", section);
                NSLog(@"[sectionInfo indexTitle] %@", [sectionInfo indexTitle]);
                NSLog(@"[sectionInfo objects] %@", [[sectionInfo objects] valueForKey:@"name"]);
                NSLog(@"array %@", tempArray);

Gives me these results:

2010-06-08 11:25:03.276 XYZApplication[5287:207] Not found Z
2010-06-08 11:25:03.277 XYZApplication[5287:207] section: 10
2010-06-08 11:25:03.279 XYZApplication[5287:207] [sectionInfo indexTitle] Z
2010-06-08 11:25:03.284 XYZApplication[5287:207] [sectionInfo objects] (
    Z
)
2010-06-08 11:25:03.284 XYZApplication[5287:207] array (
    ""
)
2010-06-08 11:25:03.285 XYZApplication[5287:207] Not found A
2010-06-08 11:25:03.285 XYZApplication[5287:207] section: 0
2010-06-08 11:25:03.286 XYZApplication[5287:207] [sectionInfo indexTitle] A
2010-06-08 11:25:03.286 XYZApplication[5287:207] [sectionInfo objects] (
    "Andy the Clown"
)
2010-06-08 11:25:03.287 XYZApplication[5287:207] array (
    "",
    Z
)
2010-06-08 11:25:03.288 XYZApplication[5287:207] Not found B
2010-06-08 11:25:03.289 XYZApplication[5287:207] section: 1
2010-06-08 11:25:03.289 XYZApplication[5287:207] [sectionInfo indexTitle] B
2010-06-08 11:25:03.290 XYZApplication[5287:207] [sectionInfo objects] (
    "Baseball Annie"
)
2010-06-08 11:25:03.291 XYZApplication[5287:207] array (
    "",
    Z,
    A
)
2010-06-08 11:25:03.291 XYZApplication[5287:207] Not found B
2010-06-08 11:25:03.292 XYZApplication[5287:207] section: 2
2010-06-08 11:25:03.292 XYZApplication[5287:207] [sectionInfo indexTitle] B
2010-06-08 11:25:03.293 XYZApplication[5287:207] [sectionInfo objects] (
    "Baseball Bugs"
)
2010-06-08 11:25:03.293 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B
)
2010-06-08 11:25:03.294 XYZApplication[5287:207] Not found D
2010-06-08 11:25:03.295 XYZApplication[5287:207] section: 3
2010-06-08 11:25:03.295 XYZApplication[5287:207] [sectionInfo indexTitle] D
2010-06-08 11:25:03.296 XYZApplication[5287:207] [sectionInfo objects] (
    ""
)
2010-06-08 11:25:03.296 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B
)
2010-06-08 11:25:03.296 XYZApplication[5287:207] Not found E
2010-06-08 11:25:03.304 XYZApplication[5287:207] section: 4
2010-06-08 11:25:03.305 XYZApplication[5287:207] [sectionInfo indexTitle] E
2010-06-08 11:25:03.306 XYZApplication[5287:207] [sectionInfo objects] (
    E
)
2010-06-08 11:25:03.306 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B,
    D
)
2010-06-08 11:25:03.307 XYZApplication[5287:207] Not found F
2010-06-08 11:25:03.307 XYZApplication[5287:207] section: 5
2010-06-08 11:25:03.308 XYZApplication[5287:207] [sectionInfo indexTitle] F
2010-06-08 11:25:03.310 XYZApplication[5287:207] [sectionInfo objects] (
    Frank
)
2010-06-08 11:25:03.310 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B,
    D,
    E
)
2010-06-08 11:25:03.311 XYZApplication[5287:207] Not found H
2010-06-08 11:25:03.311 XYZApplication[5287:207] section: 6
2010-06-08 11:25:03.312 XYZApplication[5287:207] [sectionInfo indexTitle] H
2010-06-08 11:25:03.313 XYZApplication[5287:207] [sectionInfo objects] (
    H
)
2010-06-08 11:25:03.313 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B,
    D,
    E,
    F
)
2010-06-08 11:25:03.314 XYZApplication[5287:207] Not found J
2010-06-08 11:25:03.315 XYZApplication[5287:207] section: 7
2010-06-08 11:25:03.315 XYZApplication[5287:207] [sectionInfo indexTitle] J
2010-06-08 11:25:03.316 XYZApplication[5287:207] [sectionInfo objects] (
    J
)
2010-06-08 11:25:03.316 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B,
    D,
    E,
    F,
    H
)
2010-06-08 11:25:03.317 XYZApplication[5287:207] Not found J
2010-06-08 11:25:03.317 XYZApplication[5287:207] section: 8
2010-06-08 11:25:03.329 XYZApplication[5287:207] [sectionInfo indexTitle] J
2010-06-08 11:25:03.330 XYZApplication[5287:207] [sectionInfo objects] (
    Joe
)
2010-06-08 11:25:03.330 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B,
    D,
    E,
    F,
    H,
    J
)
2010-06-08 11:25:03.335 XYZApplication[5287:207] Not found L
2010-06-08 11:25:03.336 XYZApplication[5287:207] section: 9
2010-06-08 11:25:03.336 XYZApplication[5287:207] [sectionInfo indexTitle] L
2010-06-08 11:25:03.337 XYZApplication[5287:207] [sectionInfo objects] (
    L
)
2010-06-08 11:25:03.337 XYZApplication[5287:207] array (
    "",
    Z,
    A,
    B,
    B,
    D,
    E,
    F,
    H,
    J,
    J
)


When the first element is not a match the return in the else branch will abort your search. If the first element is a match, the ‘then’ branch will break out of the loop (and I guess there is more code to provide a return value for that case?).

Maybe the return should replace the break in the ‘then’ branch?

Also, since you already have the string you want as [sectionInfo indexTitle], why manuall search for it? Do you just need to check whether it is in the list? Maybe something like this instead of the loop:

if ([tempArray containsObject: [sectionInfo indexTitle]]) {
    return [sectionInfo indexTitle];
} else {
    return @"<invalid>"; // or whatever
}


The first element of tempArray seems always to be an empty string. In your log, [sectionInfo indexTitle] is never an empty string.

Since you exit the method the first time you don't find something, you'll never get a true comparison.

Also I notice you are adding the not found [sectionInfo indexTitle] to your tempArray for every iteration you don't find it. Is that what you really want?

Also, if you ever did find a match, you don't return anything. You should be getting a compiler warning to say the method doesn't always return a value.

I'm kind of guessing here but I think you want something like:

    for (int i = 0; i < [tempArray count]; i++) // NB [nil count] returns 0 so your test for a nil tempArray is unnecessary
    {
        if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]])
        {
            NSLog(@"found");
            return [tempArray objectAtIndex:i];
        } 
        else
        {
            NSLog(@"Not found %@", [sectionInfo indexTitle]);
            NSLog(@"array %@", tempArray);
        }
    }
    [tempArray addObject:[sectionInfo indexTitle]];
    return ???? // I don't know what you want returned in this case:

Edited for the comment below:

I think I've finally understood what you are trying to do:

If you already have a section title the same as the one you are now about to deliver, you want to return nil so the section has no title. In that case, the code should look like this:

    for (int i = 0; i < [tempArray count]; i++) // NB [nil count] returns 0 so your test for a nil tempArray is unnecessary
    {
        if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]])
        {
            NSLog(@"found");
            return nil;
        } 
        else
        {
            NSLog(@"Not found %@", [sectionInfo indexTitle]);
            NSLog(@"array %@", tempArray);
        }
    }
    [tempArray addObject:[sectionInfo indexTitle]];
    return [sectionInfo indexTitle];

But

You can use NSMutableSet instead:

if ([tempSet containsObject: [sectionInfo indexTitle]])
{
    return nil;
}
else
{
    [tempSet addObject: [sectionInfo indexTitle]];
    return [sectionInfo indexTitle];
}


Are you sure the objects in tempArray are really NSString? isEqualToString will just return false if they are some other type.

Also, could you have trailing whitespace in the strings?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜