Objective-C不能在IF语句中使用stringWithContentsOfURL数据

时间:2009-10-10 12:24:07

标签: objective-c string macos url if-statement

嘿,我正在尝试ping我的服务器并检查应用程序的版本 - 到目前为止一切顺利:

 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声明不起作用,有人可以帮我吗? 感谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

您没有使用当前的条件语句检查content的内容,您正在检查它是否是有效的对象/指针而不是nil,它是(有效的)因为您刚刚声明它

使用NSString的length方法并测试0(或isEqualToString:

另请参阅此previous question以获取其他替代方案。