2011-05-20 255 views
0

以下内容将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.."); 
    } 

}

然而相比,无论字符串是什么,他们从来都不相等。 的片断,因为的NSString首先设置被清除的实际禁区前

[CommandBox setStringValue:@""]; 

应该不会有什么影响。

+0

它实际上,请参阅我的回答 – arclight 2011-05-20 17:30:21

+1

此代码没有意义。你在'SendAction:'中声明了一个局部变量,然后在另一个'General_Combinations'方法中引用该变量,在那里它将不可用。这段代码应该在运行时崩溃......这里是否有一段代码会使这项工作成为现实? – 2011-05-20 17:37:45

+0

是的,我宣布NSString在顶层。我很愚蠢,忘记通过函数传递变量。 – evdude100 2011-05-20 17:47:40

回答

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.."); 
    } 
} 
+0

字符串仍然不相等。 – evdude100 2011-05-20 17:33:17

+0

好吧,我刚刚意识到这里的问题是什么,我将编辑答案 – arclight 2011-05-20 17:34:18

+1

,您可以使用{}按钮格式化源代码,看起来好于报价;) – 2011-05-20 17:40:25