【问题标题】:Reachability in Objective-C to Swift从 Objective-C 到 Swift 的可达性
【发布时间】:2015-05-10 14:46:12
【问题描述】:

我正在尝试转换我的可达性功能。桥接 Objective-c .h 和 .m 文件后。现在我有一个函数,我调用它来检查 Objective-C 中的可达性,我想快速转换这里是 Objective-C 中的代码

-(void)reachabilityCheck
    {
        @try {
            Reachability *__autoreleasing reach = [Reachability reachabilityForInternetConnection];
            if (reach.currentReachabilityStatus) {
                self.isReachable = YES;
            }
            else
            {
                self.isReachable =  NO;
            }
        }
        @catch (NSException *exception) {
            //// [Global writeToLogFile:[exception description]];
        }
        @finally {

        }

我在 Objective-C 中这样调用这个函数

[self reachabilityCheck];

我还用 C 语言编写了 TryCatch,也包含在桥头中。我的 try catch 函数现在看起来像这样

func reachabiltyCheck()
    {
        TryCatch.try({
            // try something

            }, catch: { (error) in
                println("\(error.description)")

            }, finally: {
                // close resources

        })
    }

现在在Reachability *__autoreleasing reach = [Reachability reachabilityForInternetConnection]; 的第一行,我不知道如何快速转换它以及*_autorealeasing 在这里的含义?

编辑 1 如果我制作这样的函数,它会给我错误

func reachabiltyCheck()
    {
        TryCatch.try({
            // try something
            var reach: Reachability = Reachability.reachabilityForInternetConnection();
            var b = reach.currentReachabilityStatus();

            if(b)  // Error : Type 'NetworkStatus' does not conform to protocol 'BooleanType' 
            {
                self.isReachable = Yes;
            }
            else
            {
                self.isReachable = Yes;
            }

            }, catch: { (error) in
                println("\(error.description)")

            }, finally: {
                // close resources

        })

如果我打印 b 值,它会打印 (Enum Value) 有什么问题?

【问题讨论】:

    标签: ios objective-c swift


    【解决方案1】:

    Swift 默认使用自动引用计数,你不需要明确声明它应该是autoreleased。事实上,如果您的 Objective-C 项目使用 ARC,您也不必使用 autoreleasing

    【讨论】:

    • 如果您认为stackoverflow.com/questions/9086913/…,请阅读本文
    • @VarunNaharia 与__autoreleasing 无关。 @autoreleasepool {} 只是定义了一个运行后要释放的区域。无论您是否输入__autoreleasing,对象都会释放。
    • 那为什么_autoreleasing仍然存在?
    • @VarunNaharia: __autoreleasing 仅在指针指针或指针数组的内部级别有用。例如,NSString * __autoreleasing *NSString * __strong * 不同。你通常不应该在变量类型的顶层使用__autoreleasing
    【解决方案2】:

    经过几天的尝试,我终于将 Objective-C 函数转换为 swift

    func reachabiltyCheck()
        {
            TryCatch.try({
                // try something
                var reach: Reachability = Reachability.reachabilityForInternetConnection();
                let status:Int = reach.currentReachabilityStatus().hashValue
                if(status==1)
                {
                    self.isReachable = true;
                }
                else
                {
                    self.isReachable = false;
                }
    
                }, catch: { (error) in
                    println("\(error.description)")
    
                }, finally: {
                    // close resources
    
            })
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多