xcode Objective C - build class / objects


/ Published in: Objective C
Save to your folder(s)

Example of how to build and call a class or object


Copy this code and paste it in your HTML
  1. //----------------------------------
  2. // Person.h
  3.  
  4. #import <Foundation/Foundation.h>
  5.  
  6. @interface Person : NSObject {
  7. int age;
  8. int weight;
  9. }
  10. -(void) print;
  11. -(void) setAge: (int) a;
  12. -(void) setWeight: (int) w;
  13. @end
  14.  
  15.  
  16.  
  17. //----------------------------------
  18. // Person.m
  19. // codeTester
  20.  
  21. #import "Person.h"
  22.  
  23. @implementation Person
  24. -(void) print{
  25. NSLog(@"I am %i years old and weight %i pounds", age, weight);
  26. }
  27. -(void) setAge: (int) a{
  28. age=a;
  29. }
  30. -(void) setWeight: (int) w{
  31. weight=w;
  32. }
  33. @end
  34.  
  35.  
  36.  
  37. //----------------------------------
  38. //how to call
  39.  
  40. #import "Person.h" //dont forget to import it
  41.  
  42. Person *bucky;
  43. bucky = [Person alloc]; //give it memory
  44. bucky = [bucky init]; //allows us to defualt init metod
  45. [bucky setAge:23];
  46. [bucky setWeight:350];
  47. [bucky print];

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.