0

我正在制作一个应用程序,并尝试使用 isEqualToString 与我知道相同的字符串进行比较。但是,此代码不起作用

- (void)uui {
    array2 = [NSMutableArray arrayWithContentsOfFile:Path2];
    string = [NSString stringWithFormat:@"%@", [array2 objectAtIndex:3]];
    NSString *string2 = [NSString stringWithFormat:@"Partly Cloudy"];
    NSLog(@"%@ , %@", string, string2);
    if ([string isEqualToString:string2]) {
        NSLog(@"frack");
    }
}

这是我在日志中得到的

2013-01-02 23:13:27.512 My Sky[3788:907] Partly Cloudy  , Partly Cloudy

如您所见,它们是相同的。我在这里做错了什么?

4

1 回答 1

10

不,字符串不相等。从您的日志中:

Partly Cloudy  , Partly Cloudy

第一个字符串之后有一个额外的空格。检查此代码:

- (void)uui
{
  array2 = [NSMutableArray arrayWithContentsOfFile:Path2];
  string = (NSString *)[array2 objectAtIndex:3];
  NSString *string2 = @"Partly Cloudy";  
  NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  NSLog(@"%@ , %@", trimmedString, string2);
   if ([trimmedString isEqualToString:string2])
   {
     NSLog(@"frack");
   }
}
于 2013-01-03T04:19:56.560 回答