├── .gitignore ├── EnumReflection.playground ├── Contents.swift └── contents.xcplayground ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | ## Playgrounds 31 | timeline.xctimeline 32 | playground.xcworkspace 33 | 34 | # Swift Package Manager 35 | # 36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 37 | # Packages/ 38 | .build/ 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | # fastlane 56 | # 57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 58 | # screenshots whenever they are needed. 59 | # For more information about the recommended setup visit: 60 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 61 | 62 | fastlane/report.xml 63 | fastlane/Preview.html 64 | fastlane/screenshots 65 | fastlane/test_output 66 | -------------------------------------------------------------------------------- /EnumReflection.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | 2 | /// Adopted by an enum where each case has 3 | /// an associated struct with similar properties. 4 | protocol ReflectableEnum { 5 | func value(of propertyName: String) -> Value 6 | } 7 | 8 | extension ReflectableEnum { 9 | func value(of propertyName: String) -> Value { 10 | let selfMirror = Mirror(reflecting: self) 11 | let structChild = selfMirror.children.first! 12 | let structMirror = Mirror(reflecting: structChild.value) 13 | let property = structMirror.children.first { 14 | return $0.label == propertyName 15 | } 16 | let value: Any = property!.value 17 | return value as! Value 18 | } 19 | } 20 | 21 | enum Contact: ReflectableEnum { 22 | case addressBook(AddressBookContact) 23 | case faceBook(FaceBookContact) 24 | 25 | var id: String { return value(of: #function) } 26 | var name: String { return value(of: #function) } 27 | } 28 | 29 | struct AddressBookContact { 30 | let id: String 31 | let name: String 32 | let phoneNumber: String 33 | } 34 | 35 | struct FaceBookContact { 36 | let id: String 37 | let name: String 38 | let username: String 39 | } 40 | 41 | let bob = AddressBookContact( 42 | id: "c1", 43 | name: "Bob", 44 | phoneNumber: "5558675309") 45 | 46 | let tom = FaceBookContact( 47 | id: "c2", 48 | name: "Tom", 49 | username: "tommyboy") 50 | 51 | let contacts = [Contact.addressBook(bob), 52 | Contact.faceBook(tom)] 53 | 54 | contacts.forEach { print($0.id + " " + $0.name) } 55 | -------------------------------------------------------------------------------- /EnumReflection.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Josh Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Reflectable enums in Swift 3 2 | This repository contains the code reviewed in this blog post: 3 | 4 | https://ijoshsmith.com/2017/04/08/reflectable-enums-in-swift-3/ 5 | --------------------------------------------------------------------------------