0

I have trouble using the events my NSStream objects throw.

In obj C it is something like this:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    switch (streamEvent) {
    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        break;
    case NSStreamEventHasBytesAvailable:
        break;          
    case NSStreamEventErrorOccurred:
        NSLog(@"Can not connect to the host!");
        break;
    case NSStreamEventEndEncountered:
        break;
    default:
        NSLog(@"Unknown event");
    }
}

How do i do that in Swift? i don't understand the NSStreamEvent reference. In Obj C its an Enum And in swift its a struct. Any ideas on how to use this like the above example?

Mads Gadeberg
  • 1,429
  • 3
  • 20
  • 30

1 Answers1

0

NSStreamEvent conforms to OptionSetType in Swift 2: it's defined as a struct with static vars for each of the possible values, so you can use it like an enum.

From Enumerations:

In Swift, option sets are represented by structures conforming to the OptionSetType protocol, with static variables for each option value. Option sets behave like Swift’s Set collection type. You use the insert(_:) or unionInPlace(_:) methods to add option values, the remove(_:) or subtractInPlace(_:) methods to remove option values, and the contains(_:) method to check for an option value. You create a new option set value using an array literal, accessing option values with a leading dot (.) similar to an enumeration. An empty option set can be created from an empty array literal ([]) or by calling its default initializer.

jtbandes
  • 115,675
  • 35
  • 233
  • 266