Just begun to work in swift following Protocol Oriented Programming, I'm facing an issue working with computed variables.
I've make a protocol to check internet reachability on the whole app (instead of using a UIVIewController subclass which implement this behaviour, already done). But when I run the app crashes on access set property for a computed variable ():
And this is the code:
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)
}
}
Any idea how can I solve this issue? Thank you.