Skip to content

Instantly share code, notes, and snippets.

@diatrevolo
Created March 3, 2016 15:36
Show Gist options
  • Save diatrevolo/33e9473024ffda78f9df to your computer and use it in GitHub Desktop.
Save diatrevolo/33e9473024ffda78f9df to your computer and use it in GitHub Desktop.
import Foundation
import Alamofire
struct MyRESTClient {
let url: NSURL // this, being in a struct, will become a parameter of the initializer automatically
func getAvailableItems(completion: ((items: [MyItem]?) -> ())) {
Alamofire.request(.GET, url.absoluteString + "/items", parameters: nil)
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON { response in
if let JSON = response.result.value as? [String : AnyObject],
items = JSON["items"] as? [String] {
var returnArray = [MyItem]()
for item in items {
returnArray.append(MyItem(name: item))
}
completion(items: returnArray)
} else {
completion(items: nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment