Reachability.swift: Connection.none can cause confusion

Created on 23 Oct 2017  路  7Comments  路  Source: ashleymills/Reachability.swift

Hi,

the documentation says "isReachable has been deprecated. Use connection != .none instead".
This can cause confusion when used together with optionals. Here is an example:

Assume you store the reachability instance into an optional property:
var reachability = Reachability()

And then later you want to do something if you are not connected:

if reachability?.connection == .none {
    // show offline message, for example
}

The code inside the if statement will only be called if reachability is nil, not if it has a value and connection equals .none (because Swift checks if the optional value _reachability_ equals Optional.none). To make it work when _reachability_ is an optional property, you have to write

if reachability?.connection == Reachability.Connection.none {
    // show offline message, for example
}

Thats why it might be better to rename Connection.none to something else or mention it in the documentation.

Most helpful comment

Connection.unavailable sounds good to me 馃憤

All 7 comments

@hamchapman We should look at renaming this I think鈥β營'm struggling to think of good name. Connection.noConnection repeats "connection" so that's out. Maybe .off?

@ashleymills @hamchapman I think Connection.unavailable works best here.

@DanielStormApps Perfect!

Connection.unavailable sounds good to me 馃憤

Just faced this confusion after updating - it tricked me! 馃槃
Renaming Connection.unavailable in an upcoming release sounds great 馃憤

I encountered this issue recently. Are there plans to actually implement this fix?

I just pushed this fix - will be in the next release

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bclymer picture bclymer  路  12Comments

jet-snag picture jet-snag  路  3Comments

antonys1 picture antonys1  路  6Comments

ShayanPapershift picture ShayanPapershift  路  3Comments

rjt3662 picture rjt3662  路  3Comments