How to access a Swift enum associated value outside of a switch statement

后端 未结 5 1673
我寻月下人不归
我寻月下人不归 2021-01-31 07:08

Consider:

enum Line {
    case    Horizontal(CGFloat)
    case    Vertical(CGFloat)
}

let leftEdge             =  Line.Horizontal(0.0)
let leftMaskRightEdge             


        
5条回答
  •  死守一世寂寞
    2021-01-31 07:47

    With Swift 2 it's possible to get the associated value (read only) using reflection.

    To make that easier just add the code below to your project and extend your enum with the EVAssociated protocol.

        public protocol EVAssociated {
        }
    
        public extension EVAssociated {
            public var associated: (label:String, value: Any?) {
                get {
                    let mirror = Mirror(reflecting: self)
                    if let associated = mirror.children.first {
                        return (associated.label!, associated.value)
                    }
                    print("WARNING: Enum option of \(self) does not have an associated value")
                    return ("\(self)", nil)
                }
            }
        }
    

    Then you can access the .asociated value with code like this:

        class EVReflectionTests: XCTestCase {
                func testEnumAssociatedValues() {
                    let parameters:[EVAssociated] = [usersParameters.number(19),
    usersParameters.authors_only(false)]
                let y = WordPressRequestConvertible.MeLikes("XX", Dictionary(associated: parameters))
                // Now just extract the label and associated values from this enum
                let label = y.associated.label
                let (token, param) = y.associated.value as! (String, [String:Any]?)
    
                XCTAssertEqual("MeLikes", label, "The label of the enum should be MeLikes")
                XCTAssertEqual("XX", token, "The token associated value of the enum should be XX")
                XCTAssertEqual(19, param?["number"] as? Int, "The number param associated value of the enum should be 19")
                XCTAssertEqual(false, param?["authors_only"] as? Bool, "The authors_only param associated value of the enum should be false")
    
                print("\(label) = {token = \(token), params = \(param)")
            }
        }
    
        // See http://github.com/evermeer/EVWordPressAPI for a full functional usage of associated values
        enum WordPressRequestConvertible: EVAssociated {
            case Users(String, Dictionary?)
            case Suggest(String, Dictionary?)
            case Me(String, Dictionary?)
            case MeLikes(String, Dictionary?)
            case Shortcodes(String, Dictionary?)
        }
    
        public enum usersParameters: EVAssociated {
            case context(String)
            case http_envelope(Bool)
            case pretty(Bool)
            case meta(String)
            case fields(String)
            case callback(String)
            case number(Int)
            case offset(Int)
            case order(String)
            case order_by(String)
            case authors_only(Bool)
            case type(String)
        }
    

    The code above is from my project https://github.com/evermeer/EVReflection https://github.com/evermeer/EVReflection

提交回复
热议问题