Skip to content

Commit

Permalink
Merge pull request #2 from hyperoslo/feature/object-mapping
Browse files Browse the repository at this point in the history
Feature object mapping during parsing
  • Loading branch information
vadymmarkov committed Oct 4, 2015
2 parents eb89caa + 1656746 commit 98f8a37
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
16 changes: 9 additions & 7 deletions Source/ListComponent.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import UIKit
import Tailor
import Sugar

protocol Component {
func render() -> UIView
Expand All @@ -10,13 +12,13 @@ struct ListItem {
var image = ""
var type = ""
var uri: String?

init(title: String, subtitle: String, image: String, type: String, uri: String? = nil) {
self.title = title
self.subtitle = subtitle
self.image = image
self.type = type
self.uri = uri
init(json: JSONDictionary) {
self.title <- json.property("title")
self.subtitle <- json.property("subtitle")
self.image <- json.property("image")
self.type <- json.property("type")
self.uri <- json.property("uri")
}
}

Expand Down
23 changes: 6 additions & 17 deletions Source/Parser.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
import UIKit
import Sugar

struct Parser {

static func parse(json: [String : AnyObject]) -> [Component] {
guard let components = json["components"] as? [[String : AnyObject]] else { return [Component]() }

static func parse(json: JSONDictionary) -> [Component] {
guard let components = json["components"] as? JSONArray else { return [Component]() }
var views = [Component]()

for component in components {
if let type = component["type"] as? String,
items = component["items"] as? [AnyObject] where type == "list" {
var componentItems = [ListItem]()
for item in items {
if let title = item["title"] as? String,
subtitle = item["subtitle"] as? String,
image = item["image"] as? String,
type = item["type"] as? String {
var uri: String?
if let target = item["target"] as? [String : String] {
uri = target["uri"]
}
let listItem = ListItem(title: title, subtitle: subtitle, image: image, type: type, uri: uri)
componentItems.append(listItem)
}
guard let json = item as? JSONDictionary else { continue }
componentItems.append(ListItem(json: json))
}

let listComponent = ListComponent(items: componentItems)
views.append(listComponent)
views.append(ListComponent(items: componentItems))
}
}

Expand Down

0 comments on commit 98f8a37

Please sign in to comment.