Matthew Hui
Matthew Hui

Reputation: 3361

Could not find an overload for '!=' that accepts the supplied arguments?

Just started playing around with swift trying to write some code that detects if there is an internet connection. I am getting Could not find an overload for '!=' that accepts the supplied arguments on the last line. What am I doing wrong?

class func hasConnectivity() -> Bool {
  let reachability: Reachability = Reachability.reachabilityForInternetConnection()
  let networkStatus: NetworkStatus = reachability.currentReachabilityStatus()
  return networkStatus != NotReachable
}

Upvotes: 1

Views: 267

Answers (3)

Khanh Nguyen
Khanh Nguyen

Reputation: 11134

Try return networkStatus != .NotReachable

Upvotes: 0

Connor
Connor

Reputation: 64644

You can use the raw value of the enum:

class func hasConnectivity() -> Bool {
    let reachability: Reachability = Reachability.reachabilityForInternetConnection()
    let networkStatus: Int = reachability.currentReachabilityStatus().value
    return networkStatus != 0
}

NotReachable always has a value of 0 so you can check against that.

Upvotes: 1

fqdn
fqdn

Reputation: 2843

One way to return the result you are looking for would be to switch on networkStatus:

switch networkStatus {
case .NotReachable:
    return false
default:
    return true
}

Upvotes: 1

Related Questions