Skip to content

Instantly share code, notes, and snippets.

Created January 11, 2016 21:03
Show Gist options
  • Save anonymous/232d62d77999f5eb27cd to your computer and use it in GitHub Desktop.
Save anonymous/232d62d77999f5eb27cd to your computer and use it in GitHub Desktop.
Swift import C enum behavior
/*
For the sake of Swift import there are 3 types of enums:
1. A non-overlapping, non-option set enum
2. A non-overlapping option set
3. An overlapping option set
*/
/// C enum of type 1: A non-overlapping, non-option set
///
/// enum Foo {
/// A,
/// B
/// }
///
/// Imports in Swift to:
enum Foo {
case A
case B
}
/// C enum of type 2: A non-overlapping option set
///
/// enum Foo {
/// A = 1,
/// B = 2
/// }
///
/// Imports in Swift to:
enum Foo: Int {
case A = 1
case B = 2
}
/// This allows the developer to define the OptionSetType protocol definition
extension Foo: OptionSetType {
init(rawValue: Int) {
self = Foo.init(rawValue: rawValue) ?? Foo.A // Or maybe Foo.B, developer discresion
}
}
/// C enum of type 3: A overlapping option set
///
/// enum Foo {
/// A = 1,
/// B = 2,
/// C = 1
/// }
///
/// Imports in Swift to (existing behavior):
struct Foo: RawRepresentable, Equatable {
}
var FooA: Foo { get }
var FooB: Foo { get }
var FooC: Foo { get }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment