OC类方法到Swift

今天在做网络状态监听的时候,用到了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***的方法与普通的类方法不一样吧,这里仅作为记录,具体原因就不探究了,毕竟比较少见,而且也是个规律的事儿

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容