Monday, September 30, 2013

Stanford Course - Build a Calculator with iOS5

I went thru the Stanford iOS5 code walk thru to build the calculator.  The lecture notes can be found here.

A Few tidbits on the new stuff i learned:

Convert a double to a string - here on the brain object , send title of button pressed to performOperation method which returns a double.  On the NSString method StringWithFormat method, we convert it to a string value
double result = [self.brain performOperation:sender.currentTitle];
NSString *val = [NSString stringWithFormat:@"%g", result];
How to write a getter/setter for a property (in this case an array which get initialized prior to use)
- (NSMutableArray *) operandStack {
     if (_operandStack == nil) {
        _operandStack = [[NSMutableArray alloc] init];
     }
     return _operandStack;
 }
- (void) setOperandStack: (NSMutableArray *) operandStack {
                 _operandStack = operandStack ;
          }

Adding wrapped double in a object to an array:
NSNumber *number = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:number];
 Referencing and then removing object in an array:
NSNumber *number = [self.operandStack lastObject];
if (number)    [self.operandStack removeLastObject]
 Append to current Output String the digit value pressed:
    NSString *newDisplayText = [calculatorOutputText stringByAppendingString:buttonText] ;

Btw, I found this web site cs193p that chronicles the entire course [calculator post]. Another site.  Lastly,  on the apple iOS site  portal or Dev Center, noticed they have section on configuring web applications and things like CSS

One more thing. In the file system on your mac, there is the developer directory. This folder contains about xcode pdf file, a documentation directory, and even a code samples directory.
Related Links:
Objective C Primer
Working with Objective C
NSMutableArray Reference 
NSString Reference

No comments:

Post a Comment