Skip to content

Instantly share code, notes, and snippets.

@Pretz
Created October 7, 2014 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pretz/b96ece99effdee961760 to your computer and use it in GitHub Desktop.
Save Pretz/b96ece99effdee961760 to your computer and use it in GitHub Desktop.
// 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