Best way to enum NSString

前端 未结 7 862
广开言路
广开言路 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:46

    Alternative way to use struct:

    extern const struct AMPlayerStateReadable
    {
        __unsafe_unretained NSString *ready;
        __unsafe_unretained NSString *completed;
        __unsafe_unretained NSString *playing;
        __unsafe_unretained NSString *paused;
        __unsafe_unretained NSString *broken;
    } AMPlayerState;
    
    const struct AMPlayerStateReadable AMPlayerState =
    {
        .ready = @"READY",
        .completed = @"COMPLETE",
        .playing = @"PLAYING",
        .paused = @"PAUSED",
        .broken = @"BROKEN"
    };
    

    Then you can use like this:

    NSString *status = AMPlayerState.ready;
    

    Easy to use, readable. Would be nice if someone update/edit answer with advantages/disadvantages of this approach.

提交回复
热议问题