5

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 (): enter image description here

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.

KSR
  • 1,699
  • 15
  • 22
RFG
  • 2,880
  • 3
  • 28
  • 44
  • 1
    You run into an infinite loop because the setter calls itself again and again. – vadian Sep 08 '16 at 11:17
  • `reachability = newValue` recursively calls the `reachability` setter (same with the getter). You probably want to allow conforming types to define their own stored property `reachability` instead. – Hamish Sep 08 '16 at 11:17
  • in this case i think `set` should be using to give value to other stored variable, not to set itself, like adding a+newValue where a is stored variable, `get` is for compute something and return a value – Tj3n Sep 08 '16 at 11:20
  • 3
    A computed property cannot work – the storage for the value must be *somewhere*. And you *cannot* add a (stored) property in an extension: http://stackoverflow.com/questions/29025785/swift-why-cannot-add-store-property-in-extension-whats-the-different-between. If really necessary, you can use "associated objects" as the storage: http://stackoverflow.com/questions/24133058/is-there-a-way-to-set-associated-objects-in-swift. – So which one to choose as a duplicate? – Martin R Sep 08 '16 at 11:28
  • @MartinR Thank you, your comment has been very useful. Now it works like a charm using "associated objects". – RFG Sep 08 '16 at 11:48

0 Answers0