Best way to enum NSString

前端 未结 7 699
广开言路
广开言路 2021-02-02 13:23

Im digging for ways to enum objc object such as NSString, I remember there a new feature in a version of Xcode4+ which offering a new way to enum , but not clearly. Anyone know

7条回答
  •  一整个雨季
    2021-02-02 13:24

    Update in 2017

    Recent down votes drew my attention, and I'd like to add that enum is really easy to work with String now:

    enum HTTPMethod: String {
        case GET, POST, PUT
    }
    
    HTTPMethod.GET.rawValue == "GET" // it's true
    

    Original Answer

    Unfortunately I ended up using:

    
    #define HLCSRestMethodGet       @"GET"
    #define HLCSRestMethodPost      @"POST"
    #define HLCSRestMethodPut       @"PUT"
    #define HLCSRestMethodDelete    @"DELETE"
    typedef NSString*               HLCSRestMethod;
    

    I know this is not what OP asked, but writing actual code to implement enum seems to be an overkill to me. I would consider enum as a language feature (from C) and if I have to write code, I would come up with some better classes that does more than enum does.

    Update

    Swift version seems to be prettier, although the performance can never be as good.

    struct LRest {
        enum HTTPMethod: String {
            case Get = "GET"
            case Put = "PUT"
            case Post = "POST"
            case Delete = "DELETE"
        }
        struct method {
            static let get = HTTPMethod.Get
            static let put = HTTPMethod.Put
            static let post = HTTPMethod.Post
            static let delete = HTTPMethod.Delete
        }
    

    }

提交回复
热议问题