Objective-C不能在IF语句中使用stringWithContentsOfURL数据 [英] Objective-C can't use stringWithContentsOfURL data in IF statement

查看:122
本文介绍了Objective-C不能在IF语句中使用stringWithContentsOfURL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey, I'm trying to ping my server and check the version of the app - all goes well so far:

 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语句因某些原因无效,有人可以帮帮我吗?
谢谢。

The IF statements aren't working for some reason, can anyone help me out please? Thanks.

推荐答案

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

You are not checking the contents of content with your current conditional statement, you are checking to see if it is a valid object/pointer and not nil, which it is (valid) since you've just declared it.

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

Use NSString's length method and test for 0 (or isEqualToString:)

另见上一个问题另一个选择。

这篇关于Objective-C不能在IF语句中使用stringWithContentsOfURL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆