mattdonnelly / Swifter

[DEPRECATED] :bird: A Twitter framework for iOS & OS X written in Swift
MIT License
2.37k stars 388 forks source link

fix json to support @dynamicMemberLookup #348

Closed toya108 closed 2 years ago

toya108 commented 3 years ago

Hello. I fixed JSON type to support @dynamicMemberLookup.

@dynamicMemberLookup
public enum JSON : Equatable, CustomStringConvertible {

    ...

    public subscript(dynamicMember key: String) -> JSON {
        guard case .object(let dict) = self[key], let value = dict[key] else {
            return .invalid
        }
        return value
    }

    ...

}

↓usage

// before
cell.textLabel?.text = tweets[indexPath.row]["text"].string

// after
cell.textLabel?.text = tweets[indexPath.row].text.string

Currently, when accessing the JSON content, we need to specify the key name with [""]. With support for @dynamicMemberLookup, we can access the JSON content without [""]. And the benefits of traditional JSON will not be lost.

Swift's strict typing system and doesn't require you to constantly downcast accessed objects. It also removes the need for lots of optional chaining

Could you confirm?

(I’m Sorry if this suggestion has already been considered. I've checked the Issue and PR. But it may not be perfect...)

toya108 commented 2 years ago

I close this PR because the repository will be deprecated.