2009-10-10 25 views
1

嘿,我嘗試ping我的服務器和檢查應用程序的版本 - 一切順利,到目前爲止:的Objective-C不能在IF語句中使用stringWithContentsOfURL數據

NSURL *url = [NSURL URLWithString:@"http://thetalkingcloud.com/static/ping_desktop_app.php?v=1.1"]; 
    NSError *theNetworkError; 
    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&theNetworkError]; 
    //make sure something was returned 
    if (!content) { 
    //didn't connect 
    //my code that goes here is all fine and works 
    } else { 
//there was some content returned by the server 
//this NSLog prints out the expected data ('success' without the quotes) 
    NSLog(@"server returned data: >|%@|<", content); 
//PROBLEM: 
//neither of these two EVER become true, even when the server DOES return success or update 
    //connected, check what the server returned 
    if (content == @"success") { 
    NSLog(@"all went perfect"); 
    } else if (content == @"update") { 
    NSLog(@"version error"); 
    } 
    } 

的IF語句AREN因爲某種原因不工作,任何人都可以幫我解決問題嗎? 謝謝。

回答

2

你沒有用你當前的條件語句檢查content的內容,你檢查它是否是一個有效的對象/指針,而不是nil,因爲你剛剛宣佈它是(有效的)。

使用NSString的length方法和試驗0(或isEqualToString:

同樣看到這個previous question爲另一種選擇。

+0

非常感謝這個修好:) – 2009-10-10 12:59:59

+0

沒問題,我的第一個接受的答案:) – 2009-10-10 13:39:16

2

使用isEqualToString:方法,而不是指針相等(==)。