Objective c 目标-C可以';在IF语句中不使用stringWithContentsOfURL数据
嘿,我正在尝试ping我的服务器并检查应用程序的版本-到目前为止一切顺利:Objective c 目标-C可以';在IF语句中不使用stringWithContentsOfURL数据,objective-c,string,macos,url,if-statement,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:NSASCIIStringE
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");
}
}
NSURL*url=[NSURL URLWithString:@”http://thetalkingcloud.com/static/ping_desktop_app.php?v=1.1"];
N错误*网络错误;
NSString*content=[NSString stringWithContentsOfURL:url编码:NSASCIIStringEncoding错误:&theNetworkError];
//确保有东西被退回
如果(!内容){
//没有连接
//我的代码在这里运行良好
}否则{
//服务器返回了一些内容
//此NSLog打印出预期的数据(“成功”,不带引号)
NSLog(@“服务器返回的数据:>|%@使用isEqualToString:方法,而不是指针相等(=)。使用isEqualToString:方法,而不是指针相等(=).您不是在用当前的条件语句检查content
的内容,而是在检查它是否是有效的对象/指针,而不是nil,因为您刚刚声明了它(有效)
使用NSString的length
方法并测试0(或IsequalString:
)
另一种选择请参见此。您没有使用当前的条件语句检查内容的内容,而是检查它是否是有效的对象/指针,而不是nil,因为您刚刚声明了它(有效)
使用NSString的length
方法并测试0(或IsequalString:
)
另一个备选方案见此