Skip to content

Commit

Permalink
Fix argument json creation. Added method to add array of items.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Herzog committed Sep 14, 2016
1 parent b354eba commit b0a99cd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/SwiftAlfred.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ public class Response: JSONable {

public init() {}

public func addItem(item: Itemable) {
public func add(item: Itemable) {
items.append(item.item)
}

public func add<T: Itemable>(items: [T]) {
items.forEach(add)
}

var json: [String: Any] {
return ["items": items.map { $0.json } ]
}
Expand All @@ -23,6 +27,7 @@ public class Response: JSONable {
}

func jsonString(object: Any) -> String {
print("Try to convert \(object) to json...")
guard let d = try? JSONSerialization.data(withJSONObject: object, options: .prettyPrinted) else {
return ""
}
Expand Down Expand Up @@ -53,8 +58,8 @@ public struct ComplexArgument {
}

public enum Argument {
case simple(argument: String)
case complex(argument: ComplexArgument)
case simple(String)
case complex(ComplexArgument)

var json: Any {
switch self {
Expand Down Expand Up @@ -101,7 +106,7 @@ public struct Item: JSONable, Itemable {
j["uid"] = uid
j["title"] = title
j["subtitle"] = subtitle
j["arg"] = arg
j["arg"] = arg?.json
j["icon"] = icon?.json
if !valid { j["valid"] = valid }
j["autocomplete"] = autocomplete
Expand Down

0 comments on commit b0a99cd

Please sign in to comment.