Design with iPhone Project 2

Warning:
Content of this website may change at any time up until class meeting.


World's Fastest Introduction to Xcode and Objective-C

Preparation

A Simple Mac Application in Xcode

pxcodeObjcCmdLine3_finderXcode.jpg
pxcodeObjcCmdLine1.jpg
Fig. 1
pxcodeObjcCmdLine2hello.jpg
Fig 2
pxcodeObjcCmdLine3_helloOut.jpg
Fig 3
pxcodeObjcCmdLine4_helloOutErrCons.jpg
Fig. 4


Variables and Operators
    int x=11, y=22;
    float a=3.0, b=4.0, z;
    z= (float)(x/y) + a*b;
    NSLog(@" x=%i, z= %f \n",x,z);   
pxcodeObjcCmdLine6_manMan.jpg
Fig. 5
pxcodeObjcCmdLine6b_xterm.jpg
Fig. 6
 x=11, z= 12.000000
    NSMutableArray  *yesNo = [NSMutableArray arrayWithObjects: @"yes", @"no", nil ];
    NSLog( @"array elements = %@,%@\nor:\n%@ ",[yesNo objectAtIndex:0], [yesNo objectAtIndex:1], yesNo);
Objects, instances, and methods
pxcodeObjcCmdLine5_newClass1.jpg
Fig. 7
Class interface defined in header file
#import <Foundation/Foundation.h>
@interface tpwEmployee : NSObject {
@private
    char     * empName;
    float    empSalary;
    float    empHours;
}
-(void) setsalary: (float)sal andHours: (float)hrs;
-(float) getsalary;
@end

#import "tpwEmployee.h"

@implementation tpwEmployee

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        empName="noname";
        empSalary=1.0;
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

-(void) setsalary: (float)sal andHours: (float)hrs{
    empSalary=sal;
    empHours=hrs;
}

-(float) getsalary{
    return empSalary;
}

@end

    tpwEmployee * emp1;
    emp1 = [tpwEmployee alloc];
    emp1 = [emp1 init];
    NSLog(@"Employee salary=%f \n",[emp1 getsalary]);
    [emp1 setsalary: 123.25 andHours: 40.0];
    NSLog(@"Employee salary=%f \n",[emp1 getsalary]);
    [emp1 release];

Memory Management

Loops and Control
    for(int nn=0; nn<4; nn++)
    {
        NSLog(@"nn=%i, nn*nn=%i \n",nn, nn*nn );
    }

    int mm=0;
    while(mm<4)
    {
        NSLog(@"mm=%i, mm*mm=%i \n",mm, mm*mm );
        mm=mm+1;
    }
    int kk=1;
    switch (kk)
    {
        case 0:
            NSLog(@"case kk=0 \n" );
            break;
        case 1:
            NSLog(@"case kk=1 \n" );
            break;
        case 2:
            NSLog(@"case kk=2 \n" );
            break;
    }

    int aa=1;
    if ( aa == 1 )
        NSLog(@"aa=1 \n");
   
    int bb=2;
    if ( bb == 1 )
        NSLog(@"bb = 1 \n");
    else
        NSLog(@"bb != 1 \n");

    int count=0;
    for(int cc=0; cc<4; cc++)
    for(int dd=0; dd<4; dd++)
    {
        count=count+1;
        if(cc == dd)
            NSLog(@"at count=%i, cc=%i == dd=%i \n",count,cc,dd);
    }

Getters, Setters, and Synthesized Accessors
1. In the header of your class, after your @interface { } section, add an  @property directive for each variable that you want to create automatic getters and setters.  For our previous example:

#import <Foundation/Foundation.h>
@interface tpwEmployee : NSObject {
@private
    char     * empName;
    float    empSalary;
    float    empHours;
    float    empSalary2;
    float    empHours2;
}
@property(assign) float empSalary2;
@property(assign) float empHours2;
-(void) setsalary: (float)sal andHours: (float)hrs;
-(float) getsalary;
@end

2. In the implementation file, of your class, add a corresponding @synthesize statement right after the @implementation.  For our previous example:

#import "tpwEmployee.h"
@implementation tpwEmployee
@synthesize empsalary2, empHours2;

- (id)init
{
... etc.
3. To use the automatically synthesized accessor methods:
    tpwEmployee * emp1;
    emp1 = [tpwEmployee alloc];
    emp1 = [emp1 init];
    NSLog(@"Employee salary=%f   hours =%f \n",[emp1 empSalary2],[emp1 empHours2]);
    [emp1 setEmpSalary2: 123.25];
    [emp1 setEmpHours2: 40.0];
    NSLog(@"Employee salary=%f   hours =%f \n",[emp1 empSalary2],[emp1 empHours2]);
    [emp1 release];


    tpwEmployee * emp1;
    emp1 = [tpwEmployee alloc];
    emp1 = [emp1 init];
    NSLog(@"Employee salary=%f   hours =%f \n",emp1.empSalary2,emp1.empHours2);
    emp1.empSalary2 = 123.25;
    emp1.empHours2 = 40.0;
    NSLog(@"Employee salary=%f   hours =%f \n",emp1.empSalary2,emp1.empHours2);
    [emp1 release];
pxcodeObjcCmdLine7_synthAccess.jpg
Fig. 8


Report



Copyright 2011 by T.P. Weldon


Apple, iPhone, iPad, and Xcode  are registered trademarks of Apple Inc.