Added dynamicMemberLookup to JSON type for Swift 4.2 and later

This commit is contained in:
Caleb Kleveter 2018-06-06 11:58:15 -05:00
parent 3a6df1dfbc
commit d387b80c25
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
1 changed files with 5 additions and 1 deletions

View File

@ -1,4 +1,6 @@
// @dynamicMemberLookup #if swift(>=4.2)
@dynamicMemberLookup
#endif
public enum JSON: Codable, Equatable { public enum JSON: Codable, Equatable {
// MARK: - Cases // MARK: - Cases
@ -10,12 +12,14 @@ public enum JSON: Codable, Equatable {
indirect case object([String: JSON]) indirect case object([String: JSON])
// MARK: - Public Methods // MARK: - Public Methods
#if swift(>=4.2)
public subscript(dynamicMember member: String) -> JSON { public subscript(dynamicMember member: String) -> JSON {
switch self { switch self {
case let .object(structure): return structure[member] ?? .null case let .object(structure): return structure[member] ?? .null
default: return .null default: return .null
} }
} }
#endif
public subscript(_ key: String...) -> JSON? { public subscript(_ key: String...) -> JSON? {
do { do {