Skip to content

Instantly share code, notes, and snippets.

@elland
Last active January 26, 2017 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elland/47d73d605d2300e55dfdb23211b41ca3 to your computer and use it in GitHub Desktop.
Save elland/47d73d605d2300e55dfdb23211b41ca3 to your computer and use it in GitHub Desktop.
public enum JSON {
public typealias D = Dictionary<String, Any>
public typealias A = Array<Dictionary<String, Any>>
case dictionary(D)
case array(A)
public init(_ dictionary: [String: Any]) {
self = .dictionary(dictionary)
}
public init(_ array: [[String: Any]]) {
self = .array(array)
}
public var dictionary: D? {
get {
switch self {
case .dictionary(let d):
return d
default:
return nil
}
}
}
public var array: A? {
get {
switch self {
case .array(let a):
return a
default:
return nil
}
}
}
}
@elland
Copy link
Author

elland commented Jan 26, 2017

Usage is not ideal yet tho. Suggestions?

if let dictionary = json?.dictionary {
} else if let array = json?.array {
}

@elland
Copy link
Author

elland commented Jan 26, 2017

switch json {
case .array(let array):
case .dictionary(let dictionary):
}

Doesn't look better to me either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment