美文网首页
OC类方法到Swift

OC类方法到Swift

作者: 山已几孑 | 来源:发表于2017-07-10 11:50 被阅读0次

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

    相关文章

      网友评论

          本文标题:OC类方法到Swift

          本文链接:https://www.haomeiwen.com/subject/fjuyhxtx.html