字符串的比较

时间:2011-05-20 17:24:56

标签: objective-c nsstring string-comparison

以下将NSString设置为文本字段的StringValue。然后,在General_combinations

中比较字符串
- (IBAction)SendAction:(id)sender
{
    NSString *MyLoggerCommand = [CommandBox stringValue];
    [CommandBox setStringValue:@""];
    [[[MyLogger textStorage] mutableString] appendString: MyLoggerCommand];
    [self General_Combinations];
}

- (void)General_Combinations
{
    NSLog(@"General Combinations called..");
    if([MyLoggerCommand isEqualToString:@"this"])
    {
        NSLog(@"Matched..");
    }

}

然而,无论弦是什么,它们永远不会相等。 摘录

[CommandBox setStringValue:@""];

不应该影响任何内容,因为在清除实际框之前首先设置了NSString。

1 个答案:

答案 0 :(得分:2)

问题是当第二种方法不知道那是什么时,你正在比较MyLoggerCommand。试试这段代码:

-(IBAction)SendAction:(id)sender {

    NSString *myLoggerCommand = [CommandBox stringValue];
    [[[MyLogger textStorage] mutableString] appendString: myLoggerCommand];
    [self General_Combinations:myLoggerCommand];
    [CommandBox setStringValue:@""];  
}

-(void)General_Combinations:(NSString *)aString {

    NSLog(@"General Combinations called..");
    if([aString isEqualToString:@"this"])
    {
        NSLog(@"Matched..");
    }
 }