iOS 8 HealthKit Santiapps Marcio Valenzuela

HealthKit for iOS8: Part 4

Ok so the Journal view controller was quite simple.  Let’s take another quick break by looking at an even simpler view controller, the FoodPicker: import Foundation import UIKit import HealthKit class FoodPickerViewController: UITableViewController { var selectedFoodItem: FoodItem? let FoodPickerViewControllerTableViewCellIdentifier:NSString = “cell” let FoodPickerViewControllerUnwindSegueIdentifier:NSString = “FoodPickerViewControllerUnwindSegueIdentifier” var foodItems: NSArray = NSArray() var energyFormatter: NSEnergyFormatter { var energyFormatter: NSEnergyFormatter? var onceToken: dispatch_once_t = 0 dispatch_once(&onceToken, { energyFormatter = NSEnergyFormatter() energyFormatter?.unitStyle = NSFormattingUnitStyle.Long energyFormatter?.forFoodEnergyUse = true energyFormatter?.numberFormatter.maximumFractionDigits = 2 }) return energyFormatter! } var  healthStore:HKHealthStore? } Here we are looking at imports, subclassing UITableViewController and property declarations. Our first property is selectedFoodItem, which will be set to whatever value the user selects from… Read More

Continue Reading

Swift Closures Quick Reference: Part 2

Let’s analyze some useful applications of closures in everyday code! Ok so let’s take a look at a real life function that uses a closure.  A typical closure is a completion handler.  A completion handler is a parameter, just like any other, that is passed into a function as a closure.  When that code gets executed, the completion handler gets filled.  Then your function will use the value of that completion handler to do something useful. Here are some typical uses of closures in API’s.  The typical one is UIView.animateWithDuration.  Open Xcode and start writing UIView.anim… and it will autocomplete for you with this: UIView.animateWithDuration(<duration: NSTimeInterval>, animations: <() -> Void()… Read More

Continue Reading

Swift Closures Quick Reference: Part 1

Blocks/Closures are confusing!  They’re confusing because its a bit abstract.  Most tutorials cover how a block is declared and used. Sometimes blocks or closures can me even more confusing… A block is a bunch of code wrapped up in a {}. You can 2 either of 2 things with them: A.  You can assign that block of code to a variable. (this is where completionHandlers, also a confusing concept, fit in) B.  You can use that block of code directly Let’s take a look at assigning it to something.  No doubt you have seen a construct like: var someName = “Mars” or var hisAge = 39 This would be considered hardcoding… Read More

Continue Reading

Swift is Confusing: Classes, Structures, Designated Initializers, Instance Methods, Type Methods, Functions, Methods, Convenience Initializers & External Parameter Names

Class vs Structs Both: Store values, initialize, subscripts, extensible & protocol Class can inherit, de-initialize, reference counting & typecast Functions vs Methods Methods are FUNCTIONS INSIDE A CLASS Functions can be inside or OUTSIDE A CLASS! Cannot use functionName(param1,param2) to call a function declared inside a class {} Methods: It is implicitly passed the object for which it was called It is able to operate on data that is contained within the class Instance Methods vs Type Methods (Instance Method vs Class Methods I think) Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for… Read More

Continue Reading