今天在做网络状态监听的时候,用到了Reachability,但是在创建Reachability 的时候遇到了问题,Reachability是OC版本的,使用位置在swift中,
Reachability.h 中声明如下:
/*!
* Use to check the reachability of a given host name.
*/
+ (instancetype)reachabilityWithHostName:(NSString *)hostName;
/*!
* Use to check the reachability of a given IP address.
*/
+ (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress;
/*!
* Checks whether the default route is available. Should be used by applications that do not connect to a particular host.
*/
+ (instancetype)reachabilityForInternetConnection;
使用时
self.internetReachability = [Reachability reachabilityForInternetConnection];
self.internetReachability = [Reachability reachabilityWithHostName:@"http://www.baidu.com"];
Swift 中,按照简单的转换应该是Reachability. reachabilityForInternetConnection 才对,
但是失败了,其使用方式是这样的:
internetReachability = Reachability(hostName: "")
internetReachability = Reachability(address: UnsafePointer(bitPattern: 0))
internetReachability = Reachability.forInternetConnection()
大概是因为*** instancetype***的方法与普通的类方法不一样吧,这里仅作为记录,具体原因就不探究了,毕竟比较少见,而且也是个规律的事儿