swift中协议扩展中的计算变量

时间:2016-09-08 11:13:54

标签: ios swift xcode afnetworking swift-protocols

刚开始按照面向协议的编程快速开始工作,我正面临着使用计算变量的问题。

我已经制定了一个协议来检查整个应用程序的互联网可达性(而不是使用实现此行为的UIVIewController子类,已经完成)。但是,当我在计算变量()的访问集属性上运行应用程序崩溃时: enter image description here

这是代码:

import UIKit

protocol ReachabilityProtocol: class {
    var reachability: NetReach? { get set }

    func startReachability()
    func stopReachability()

    func internetIsAccessible()
    func internetIsNotAccessible()
}


extension ReachabilityProtocol where Self: UIViewController {

    var reachability: NetReach? {
        get { return reachability }
        set { reachability = newValue }
    }

    func startReachability() {

        do {
            reachability = try NetReach.reachabilityForInternetConnection()
        } catch {
            print("Unable to create Reachability")
            return
        }

        NSNotificationCenter.defaultCenter().addObserverForName(ReachabilityChangedNotification, object: reachability, queue: nil, usingBlock: { notification in

        let reachable = notification.object as! NetReach

           if !reachable.isReachable() {
               self.internetIsNotAccessible()
           } else {
               self.internetIsAccessible()
           }
        })

       do {
           try reachability?.startNotifier()
       } catch {
           self.internetIsNotAccessible()
           print("Could not start reachability notifier")
       }
   }

   func stopReachability() {
       reachability!.stopNotifier()
       NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: reachability)
   }
}

知道如何解决这个问题?谢谢。

0 个答案:

没有答案