pableiros
pableiros

Reputation: 16032

Error using Objective C typedef enum in Swift 3

I try to use Reachability class provided by Apple.

It has an Objective-C enum like this:

typedef enum : NSInteger {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
} NetworkStatus;

And in Swift I'm doing something like this:

if let networkReachability = notification.object as? Reachability {
    let remoteHostStatus = networkReachability.currentReachabilityStatus()
    let center = NotificationCenter.default
    var notificationKey: String

    if (remoteHostStatus == NetworkStatus.NotReachable) {
        ...
    }
}

But I get the error:

Type 'NetworkStatus' has no member 'NotReachable'

What am I doing wrong? How can I use that Objective-C enum in Swift?

Upvotes: 3

Views: 1160

Answers (2)

matt
matt

Reputation: 535096

typedef enum : NSInteger {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
} NetworkStatus;

This is an "ordinary" C enum. So it arrives into Swift with no namespacing; the names NotReachable, ReachableViaWiFi, and ReachableViaWWAN can be used directly. Note that there is no preceding dot (.).

Upvotes: 5

Rob
Rob

Reputation: 437482

If you define your enum like follows, it will be accessible as NetworkStatus.NotReachable, etc.:

typedef NS_ENUM(NSInteger, NetworkStatus) {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
};

Upvotes: 1

Related Questions