Skip to content

Instantly share code, notes, and snippets.

@aGupieWare
aGupieWare / Swift_client_request_inventory_1.txt
Created November 6, 2014 21:56
Swift client, request inventory method, take 1 . . .
// MARK: - Networking
func requestInventory(endPointURL : String, responseHandler : (error : NSError? , items : Array<PlumbingSupplyItem>?) -> () ) -> () {
// Url session request goes here //
responseHandler( error: nil, items: nil)
}
@aGupieWare
aGupieWare / Swift_client_view_did_appear.txt
Created November 6, 2014 22:08
Swift client, viewDidAppear method . . .
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.requestInventory(self.inventoryUrlEndpoint, { (error, items) -> () in
// present processed url session response results to the user –- update UI code //
})
}
@aGupieWare
aGupieWare / Swift_client_request_inventory_2.txt
Created November 6, 2014 22:11
Swift client, request inventory method, take 2 . . .
let url:NSURL = NSURL(string: endPointURL)!
let task = self.urlSession.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
println(response)
responseHandler( error: nil, items: nil)
})
task.resume()
@aGupieWare
aGupieWare / Swift_client_parse_json.txt
Created November 7, 2014 18:50
Swift client, parse JSON snippet . . .
var jsonParseError: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonParseError) as NSDictionary
println(jsonResult)
@aGupieWare
aGupieWare / swift_client_inventory_items.txt
Last active August 29, 2015 14:09
Swift client inventory items method . . .
func inventoryItems(data: NSData) -> (Array<PlumbingSupplyItem>) {
var jsonParseError: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonParseError) as NSDictionary
return [jsonResult]
}
@aGupieWare
aGupieWare / swift_client_print_inventory_items.txt
Created November 10, 2014 23:57
Swift client, print inventory items array . . .
var inventoryItems = self.inventoryItems(data)
println(inventoryItems)
@aGupieWare
aGupieWare / swift_client_refined_inventory.txt
Created November 11, 2014 00:00
Swift client refined JSON response . . .
var rawInventoryItems = jsonResult["data"] as Array<Dictionary<String,String>>
var refinedInventoryItems : Array<PlumbingSupplyItem> = []
for itemDict in rawInventoryItems {
var item : PlumbingSupplyItem = PlumbingSupplyItem(bsn_id: itemDict["id"], bsn_name: itemDict["name"], bsn_image: itemDict["image"], bsn_description: itemDict["description"])
refinedInventoryItems.append(item)
}
return refinedInventoryItems
@aGupieWare
aGupieWare / swift_data_source_property.txt
Created November 11, 2014 00:07
Swift data source property . . .
var dataSource : Array<PlumbingSupplyItem> = []
@aGupieWare
aGupieWare / swift_client_override_boilerplate.txt
Created November 11, 2014 00:10
Swift client, override boilerplate methods . . .
override func numberOfSectionsInTableView(tableView: (UITableView!)) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: (UITableView!), numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return self.dataSource.count
}
@aGupieWare
aGupieWare / swift_client_async_dispatch.txt
Created November 11, 2014 00:15
Swift client, async dispatch . . .
self.dataSource = self.inventoryItems(data)
dispatch_async(dispatch_get_main_queue()!, { () -> Void in
self.tableView.reloadData()
self.webActivityIndicator.hidden = true
})