代码之家  ›  专栏  ›  技术社区  ›  Oh Danny Boy

目标C:比较两个不正常工作的字符串

  •  1
  • Oh Danny Boy  · 技术社区  · 14 年前
    - (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(@"found");
                    break;
                } else
                    {
                        NSLog(@"Not found %@", [sectionInfo indexTitle]);
                        [tempArray addObject:[sectionInfo indexTitle]];
                        NSLog(@"array %@", tempArray);
                        return [tempArray objectAtIndex:i];
                    }
            }
        } 
    }
    

    if语句中字符串的比较永远不会解析为true。为了测试目的,示例数据有两个重复的实例。注释行是一个替代行,尽管我认为不正确,但尝试将节与temparray中的字符串进行比较。

    我做错什么了?

    而且,所有数据都是大写字母,所以比较不是大小写的问题。

    编辑:

    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);
    

    给我这些结果:

    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
    )
    
    3 回复  |  直到 12 年前
        1
  •  1
  •   JeremyP    14 年前

    temparray的第一个元素似乎总是空字符串。在日志中,[sectioninfo indextitle]绝不是空字符串。

    因为你第一次找不到东西就退出了这个方法,你永远不会得到一个真正的比较。

    另外,我注意到您在每次未找到的迭代中,都会将未找到的[sectioninfo indextitle]添加到您的temparray中。这就是你真正想要的吗?

    而且,如果你找到了一个匹配的,你不会返回任何东西。你应该得到一个编译器警告,说这个方法并不总是返回一个值。

    我有点猜测,但我认为你想要:

        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:
    

    编辑评论如下:

    我想我终于明白你想做什么了:

    如果你已经有了一个和你现在要交付的相同的部分标题,你想返回nil,这样该部分就没有标题了。在这种情况下,代码应该如下所示:

        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];
    

    但是

    您可以使用nsmutableset代替:

    if ([tempSet containsObject: [sectionInfo indexTitle]])
    {
        return nil;
    }
    else
    {
        [tempSet addObject: [sectionInfo indexTitle]];
        return [sectionInfo indexTitle];
    }
    
        2
  •  2
  •   Chris Johnsen    14 年前

    当第一个元素不匹配时, return 在else分支中,将中止搜索。如果第一个元素是匹配的,那么__然后__分支将跳出循环(我猜还有更多的代码来为这种情况提供返回值?).

    也许 返回 应该替换 break 在_然后_稹分支中?

    另外,因为您已经有了所需的字符串 [sectionInfo indexTitle] ,为什么要手动搜索?你只需要检查它是否在列表中吗?可能是这样的,而不是循环:

    if ([tempArray containsObject: [sectionInfo indexTitle]]) {
        return [sectionInfo indexTitle];
    } else {
        return @"<invalid>"; // or whatever
    }
    
        3
  •  1
  •   James    14 年前

    你确定里面的东西 tempArray 真的 NSString ? isEqualToString 如果它们是其他类型,则返回false。

    另外,字符串中可以有尾随空格吗?