Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in parsing #1

Merged
merged 1 commit into from
Aug 22, 2016
Merged

Adding in parsing #1

merged 1 commit into from
Aug 22, 2016

Conversation

eriksargent
Copy link

Parsing improvements to LKAPI!

All (probably) data models that are serialized from JSON data (now represented as ModelDict; Use it, it's way easier to type than [String: AnyObject]) should respond to the ModelType protocol.

This will cause all models to get the static parse function that will initialize (with data: ModelDict) that type by default, and will need to create an initializer to handle the JSON data init(data: ModelDict).

Here's where this gets awesome. No more if lets to serialize data into your models! Instead, use the parse function on the dictionary! There's three versions of the function:

  • parse<T>(key: String, or fallback: T) -> T - Use this for primitive types. So to parse the id from a dictionary with the default of 0 use id = dict.parse("id", or 0)
  • parse<T where T: Parseable>(key: String, type: T.Type) -> T? - Use this for parsing an AnyObject to a Parseable type. So if you want to convert a date string into a date, create a parseable extension on NSDate and define parse(data: AnyObject) -> Parseable? that converts the string to an NSDate.
  • parse<T where T: ModelType>(type: T.Type) -> T? - Use this to parse a dictionary to a ModalType

Please checkout the ParsingTests.swift file for a bunch of examples on how to use the new parsing improvements.

@abrahamdone abrahamdone merged commit 40965e8 into master Aug 22, 2016
@abrahamdone abrahamdone deleted the Parsing branch August 22, 2016 18:59
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.

3 participants