Doubts from CS193P Lectures 1-4

a) Presence1 with TableViewController.  Why the initWithNibName method in the DetailController.m contains no specific values except for the title setter? Couldnt i just set this title in the viewDidLoad?

b) Why if we call
NSMutableString *string = [[NSMutableString alloc] init]; we must later release it.  But if we called NSMutableString *string = NSMutableString string]; then we dont have to cause itll be autoreleased in the pool.  If this is so, why would we even bother calling it the first way?c) MoveMeApp from Apple Dev Center.  The placard.png image is ‘imported’ by the UIImage line in the init.  And the a CGRect frame is created based on the size of that image.  Is this app importing that image into the CGRect frame created or is it importing the image and ‘tracing a frame with the exact features of the imported png image? In other words, is it actually re-drawing the png or is it just placing it inside a frame container?

d) On Warmup 2, i get the Dictionary was mutated while being enumerated.  Ok, but isnt it a MUTABLE type dictionary? I guess that means you can change its contents later, just not during a thread operation.  Maybe something to do with the fact that the dictionary would have had to be copied?

e) in the debugger window, i dont know what the register window tells you? in debugging splitter2 i get 0×20(%edx),%edi with a red arrow left of it and so i step into it and i get a EX_BAD_ACCESS.  I found the error in code; NSString *string didnt have an assignment so the function isnt passed a value.  my question is, how does debugger point me to that mistake IN the code? cause luckily this code was short, what if it were HUGE?!  im missing something obviously.

f) i cant find anything wrong with warmup 3

g) in splitter3 i found the assign vs retain property.  I know that retain bumps up the reference count on words object vs assign which changes its value.  but the wrong example is #3 where assign is used.  and since this is NSArray object, it cant be assigned a value?  so i tried converting it to a NSMutableArray and it didnt work.  Because i think i needed then to define a setter method for it but didnt know how?

Overview of an Iphone App for NOOBs

This is based on the stanford assignment 2B but its the basic layout for the design using the MVC. MVC is a template for design that you can use in iphone dev.

The model is where you define a new class of object and the methods it will perform. In this case, simply the contraints of min and max sides as well as the angles, name and description methods of a polygon.

Then the controller files define the class for all the interface controls and the methods they use.

Basically these controls will communicate with the model objects to update them of changes from the user actions in the view.

The model will then check itself and notify the controller of its methods and the controller will turn around and update the view objects in the xib.


The model will have the definition of an animal.  If its a mammal or reptile, what color it is, does it have hair, how many legs or extremities.

In the view you will display an animal with data received from the controller.  Also the controller will present the view  to the interface for him to select different animals from the screen.

The controller will translate user selections to go fetch the animal properties according to user selection and update the view the user sees.

Saving HealthKit Data & Closures

iOS 8 HealthKit Santiapps Marcio Valenzuela
iOS 8 HealthKit

This code bit saves:

  healthKitStore.saveObject(bmiSample, withCompletion: { (success, error) -> Void in
    if( error != nil ) {
      println("Error saving BMI sample: \(error.localizedDescription)")
    } else {
      println("BMI sample saved successfully!")

The method signature is:

saveObject(object: HKObject!, withCompletion completion: ((Bool, NSError!) -> Void)!)

This method takes an HKObject which is bmiSample

and it takes a completion closure which itself takes a bool & error and returns void.

So in our method call, we pass in the bmiSample as the HKObject and for the success and error completion block we say:

if error is NOT nil then log that error’s description,

else log that the bmiSample was saved successfully.



This code bit reads:

// 2. Call the method to read the most recent weight sample
self.healthManager?.readMostRecentSample(sampleType, completion: { (mostRecentWeight, error) -> Void in
  if( error != nil )
    println("Error reading weight from HealthKit Store: \(error.localizedDescription)")
  var weightLocalizedString = self.kUnknownString;
  // 3. Format the weight to display it on the screen
  self.weight = mostRecentWeight as? HKQuantitySample;
  if let kilograms = self.weight?.quantity.doubleValueForUnit(HKUnit.gramUnitWithMetricPrefix(.Kilo)) {
    let weightFormatter = NSMassFormatter()
    weightFormatter.forPersonMassUse = true;
    weightLocalizedString = weightFormatter.stringFromKilograms(kilograms)
  // 4. Update UI in the main thread
  dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.weightLabel.text = weightLocalizedString

Here we are calling the .readMostRecentSample method which has this signature:

func readMostRecentSample(sampleType:HKSampleType , completion: ((HKSample!, NSError!) -> Void)!)

This method takes an HKSampleType which is sampleType

and it takes a completion closure which itself takes an HKSample & error and returns void.

So in our method call, we pass in the sampleType as the HKSample and for the success and error completion block we say:

if error is NOT nil then log that error’s description,

else get that mostRecentWeight as self.weight, format it and set it as the label’s text.

How does this method get the actual value from the HKHealthStore?  Inside itself, it executes an HKQuery which itself says this:

let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor])
    { (sampleQuery, results, error ) -> Void in
      if let queryError = error {
      // Get the first sample
      let mostRecentSample = results.first as? HKQuantitySample
      // Execute the completion closure
      if completion != nil {
  // 5. Execute the Query

Give me:

a.  sampleType

b.  a predicate, limit, sortDescriptors

c. and a completion closure which takes a sampleQuery, results & error

such that;

if error it NOT nil, set the readMostRecentSample method’s completion closure to nil,error

otherwise set the completion closure with the results and no error.