2011-05-20 159 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