NSHost“当前主机"未识别 [英] NSHost "currentHost" not recognized

查看:109
本文介绍了NSHost“当前主机"未识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用此代码来了解我的IP,但是我收到2条警告,目前无法修复.我还发现了这篇文章:使用NSHost访问IP地址

i'd like to use this code to know my ip, but i got 2 warnings that i can't fix for now. I also found this post : Accessing IP Address with NSHost

但是我只是想了解,如果有人有答案,为什么这段代码不起作用?

but i just wanted to understand why this code does not work, if anyone has an answer?

这是我的代码:

      -(NSString*)getAddress {
            NSString *iphone_ip = [NSString initWithString:@"127.0.0.1"];
            NSHost* myhost =[NSHost currentHost];
            if (myhost)
            {
                NSString *ad = [myhost address];
                if (ad)
                    strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
            }
            return [NSString stringWithFormat:@"%s",iphone_ip]; 
        }

第一个警告打开:

NSHost * myhost = [NSHost currentHost]

NSHost* myhost =[NSHost currentHost]

,表示未识别currentHost. 第二个是

, saying that currentHost is not recognised. The second one is on

NSString * ad = [myhost地址];

NSString *ad = [myhost address];

不兼容的obj-c类型,初始化'struct NSData ",预期为'struct NString "

" incompatible obj-c types initializing 'struct NSData ", expected 'struct NString" "

我可以想象,当第一个警告解决后,第二个警告可能会消失...

I can imagine the second warning might disappear when the first warning is resolved...

感谢您的帮助

保罗

推荐答案

从此处的文档中可以看到,它记录在OS X库中.

As you can see from the documentation here, it's documented under the OS X library.

NSHost

它实际上是iPhone上的私有API.您仍然应该可以使用它,但是会收到编译器警告.

It's actually a private API on the iPhone. You should still be able to use it, but you'll get compiler warnings.

如果需要查找IP地址,则可以使用以下URL使用NSURLRequest和NSURLConnection: WhatIsMyIP API

If you need to find out your IPAddress, you can use a NSURLRequest and NSURLConnection using this URL: WhatIsMyIP API

该页面专门供程序员使用.他们要求您每300秒对它执行一次ping操作.您可以在此处找到常见问题解答:常见问题解答

That page is there specifically for programmers to use. They ask that you ping it no more than once every 300 seconds. You can find a FAQ here: FAQ

这篇关于NSHost“当前主机"未识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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