Skip to content

Adding Support for Custom JSONSubscriptType - #376

Merged
zhigang1992 merged 2 commits into
SwiftyJSON:masterfrom
zhigang1992:AddingJSONSubscriptKey
Nov 1, 2015
Merged

Adding Support for Custom JSONSubscriptType#376
zhigang1992 merged 2 commits into
SwiftyJSON:masterfrom
zhigang1992:AddingJSONSubscriptKey

Conversation

@zhigang1992

Copy link
Copy Markdown
Contributor

#375

So that we can use enum as keys like this:

enum J : String {
    case Id = "id"
    case ChannelId = "channel_id"
    case ChannelTitle = "channel_title"
    case Title = "title"
    case Template = "template"
    case Articles = "articles"
}

extensions J : JSONSubscriptType {
    var jsonKey:JSONSubscriptKey {
        return JSONSubscriptKey.Key(self.rawValue)
    }
}

And use it like this

let idNumber = j[J.ChannelId].number ?? j[J.Id].number ?? NSNumber(int: 0)

@zhigang1992

Copy link
Copy Markdown
Contributor Author

Sorry about deleting the trailing spaces.

I can revert them if it's too much trouble.

:)

zhigang1992 added a commit that referenced this pull request Nov 1, 2015
Adding Support for Custom JSONSubscriptType
@zhigang1992
zhigang1992 merged commit 91d77eb into SwiftyJSON:master Nov 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants