0
import Foundation
import Reachability

class CMNetworkManager: NSObject {

    let reachability = try! Reachability()
    
    override init() {
        super.init()
        }

    class func isReachable() -> Bool {
        // deprecated
        // return self.sharedInstance.reachability.isReachable
        return reachability.connection != .none
    }

    class func isReachableViaWifi() -> Bool {
        //deprecated
        //return self.sharedInstance.reachability.isReachableViaWiFi
        return reachability.connection == .wifi
    }

    class func isReachableViaWWAN() -> Bool {
        // deprecated
        // return self.sharedInstance.reachability.isReachableViaWWAN
        return reachability.connection == .cellular
    }
    class func testCodeFromDocumentation () {
    reachability.whenReachable = { reachability in
        if reachability.connection == .wifi {
            print("Reachable via WiFi")
        } else {
            print("Reachable via Cellular")
        }
    }
    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }

    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}
}

Whenever reachability.something is used this error shows up in Xcode:

Instance member 'reachability' cannot be used on type 'CMNetworkManager'


NOTE:

The answers mentioned here do not apply to my example. ( make sure you're not attempting to modify the class rather than the instance). I am modifying an instance here (reachability)

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
AG_HIHI
  • 1,705
  • 5
  • 27
  • 69

1 Answers1

2

The problem is that you are trying to access an instance property from a type method. You either need to make all your methods instance methods or make the property a type property.

static let reachability = try! Reachability() will fix your issue, since that makes the property a type property.

Also, there's no need for the NSObject inheritance in Swift.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116