Created
October 7, 2014 02:52
-
-
Save Pretz/b96ece99effdee961760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import UIKit | |
struct ApiResponse { | |
let success: Bool | |
let values: [[NSObject: AnyObject]] | |
} | |
class BaseModel: NSObject { | |
class func arrayFromApi<T: BaseModel>(response: ApiResponse, success: ([T] -> Void), failure: (NSError -> Void)) { | |
if response.success { | |
success(response.values.map { T(values: $0) }) | |
} | |
} | |
required init(values: [NSObject: AnyObject]) { | |
super.init() | |
setValuesForKeysWithDictionary(values) | |
} | |
} | |
class SubModel: BaseModel { | |
dynamic var prop1: String! | |
override var description: String { | |
return "SubModel(prop1: \(prop1))" | |
} | |
} | |
let response = ApiResponse(success: true, values: [["prop1": "something"], ["prop1": "nothing"]]) | |
SubModel.arrayFromApi(response, success: { (models: [SubModel]) in | |
println(models) | |
}, failure: { error in | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment