Skip to content

Commit

Permalink
Return strong types Entities from downloadJSON method
Browse files Browse the repository at this point in the history
  • Loading branch information
savelichalex committed Aug 25, 2016
1 parent dfa9b12 commit c11f9aa
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions SerialsManager/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import Cocoa
import SwiftyDropbox
import PromiseKit

typealias EntityJSON = [String: AnyObject]
typealias JSON = [String: AnyObject]
struct EntityJSON {
let title: String
let path: String
}
typealias Entities = [EntityJSON]

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand Down Expand Up @@ -80,7 +85,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

}

func downloadJSON(path: String) -> Promise<[EntityJSON]> {
func downloadJSON(path: String) -> Promise<Entities> {
return Promise { resolve, reject in
guard let client = Dropbox.authorizedClient else {
reject(SerialsError.Unauthorized)
Expand All @@ -105,12 +110,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as? [EntityJSON]
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as? [JSON]
guard json != nil else {
reject(SerialsError.DownloadError(description: "Convert error"))
return
}
resolve(json!)
resolve(json!.map {
let title = $0["title"] as! String
let path = $0["path"] as! String
return EntityJSON(title: title, path: path)
})
} catch {
reject(SerialsError.NotValid)
}
Expand Down

0 comments on commit c11f9aa

Please sign in to comment.