【问题标题】:Objective-C can't use stringWithContentsOfURL data in IF statementObjective-C 不能在 IF 语句中使用 stringWithContentsOfURL 数据
【发布时间】:2010-12-05 14:10:18
【问题描述】:

嘿,我正在尝试 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 语句由于某种原因无法正常工作,有人可以帮帮我吗? 谢谢。

【问题讨论】:

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


    【解决方案1】:

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

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

    另请参阅previous question 以了解另一种选择。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        相关资源
        最近更新 更多