/ Published in: Objective C

Example of how to build and call a class or object
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//---------------------------------- // Person.h #import <Foundation/Foundation.h> int age; int weight; } -(void) print; -(void) setAge: (int) a; -(void) setWeight: (int) w; @end //---------------------------------- // Person.m // codeTester #import "Person.h" @implementation Person -(void) print{ NSLog(@"I am %i years old and weight %i pounds", age, weight); } -(void) setAge: (int) a{ age=a; } -(void) setWeight: (int) w{ weight=w; } @end //---------------------------------- //how to call #import "Person.h" //dont forget to import it Person *bucky; bucky = [Person alloc]; //give it memory bucky = [bucky init]; //allows us to defualt init metod [bucky setAge:23]; [bucky setWeight:350]; [bucky print];
Comments
