0% found this document useful (0 votes)
40 views22 pages

src8 Malan

This document contains source code for several Cocoa/Objective-C classes that define Student objects with various implementations of properties and initialization methods over multiple files. The classes demonstrate using properties, synthesis, and custom initialization methods to encapsulate the data and behavior of a Student.

Uploaded by

David Leonard
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views22 pages

src8 Malan

This document contains source code for several Cocoa/Objective-C classes that define Student objects with various implementations of properties and initialization methods over multiple files. The classes demonstrate using properties, synthesis, and custom initialization methods to encapsulate the data and behavior of a Student.

Uploaded by

David Leonard
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

src8-malan/Nib1/Nib1/Controller.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.

// // // // // // // // // //

Controller.h Nib1 David J. Malan Harvard University [email protected] Demonstrates a Window-based Application implemented with a nib, plus IBAction and IBOutlet.

#import <Foundation/Foundation.h> @interface Controller : NSObject { } @property (nonatomic, retain) IBOutlet UITextField *textField; - (IBAction)done:(id)sender; - (IBAction)go:(id)sender; @end

src8-malan/Nib1/Nib1/Controller.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. // // // // // // // // // //

Controller.m Nib1 David J. Malan Harvard University [email protected] Demonstrates a Window-based Application implemented with a nib, plus IBAction and IBOutlet.

#import "Controller.h" @implementation Controller @synthesize textField=_textField; - (IBAction)done:(id)sender { // hide keyboard [sender resignFirstResponder]; } - (IBAction)go:(id)sender { // hide keyboard [self.textField resignFirstResponder]; // show alert NSString *s = [NSString stringWithFormat:@"Hello, %@", self.textField.text]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:s delegate:nil cancelButtonTitle:@"Thanks!" otherButtonTitles:nil]; [alert show]; [alert release]; } - (void)dealloc { [_textField release]; [super dealloc]; } @end

src8-malan/Nib1/Nib1/Nib1AppDelegate.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. // // // // // // // // // //

Nib1AppDelegate.h Nib1 David J. Malan Harvard University [email protected] Demonstrates a Window-based Application implemented with a nib, plus IBAction and IBOutlet.

#import <UIKit/UIKit.h> #import "Controller.h" @interface Nib1AppDelegate : NSObject <UIApplicationDelegate> { } @property (nonatomic, retain) IBOutlet Controller *controller; @property (nonatomic, retain) IBOutlet UIWindow *window; @end

src8-malan/Nib1/Nib1/Nib1AppDelegate.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. // // // // // // // // // //

Nib1AppDelegate.m Nib1 David J. Malan Harvard University [email protected] Demonstrates a Window-based Application implemented with a nib, plus IBAction and IBOutlet.

#import "Nib1AppDelegate.h" @implementation Nib1AppDelegate @synthesize controller=_controller; @synthesize window=_window; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self.window makeKeyAndVisible]; return YES; } - (void)dealloc { [_controller release]; [_window release]; [super dealloc]; } @end

src8-malan/Students1/Students1/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. // // // // // // // // // //

Student.h Students1 David J. Malan Harvard University [email protected] Declares a student.

#import <Foundation/Foundation.h> @interface Student : NSObject { @public int age; NSString *name; } @end

src8-malan/Students1/Students1/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. // // // // // // // // // //

Student.m Students1 David J. Malan Harvard University [email protected] Defines a student.

#import "Student.h" @implementation Student @end

src8-malan/Students2/Students2/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. // // // // // // // // // //

Student.h Students2 David J. Malan Harvard University [email protected] Declares a student with getters and setters.

#import <Foundation/Foundation.h> @interface Student : NSObject { int _age; NSString *_name; } - (int)age; - (void)setAge:(int)age; - (NSString *)name; - (void)setName:(NSString *)name; @end

src8-malan/Students2/Students2/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. // // // // // // // // // //

Student.m Students2 David J. Malan Harvard University [email protected] Defines a student with getters and setters.

#import "Student.h" @implementation Student - (int)age { return _age; } - (void)setAge:(int)age { _age = age; } - (NSString *)name { return _name; } - (void)setName:(NSString *)name { if (_name != name) { [_name release]; _name = [name copy]; } } - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students3/Students3/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. // // // // // // // // // //

Student.h Students3 David J. Malan Harvard University [email protected] Declares a student with properties.

#import <Foundation/Foundation.h> @interface Student : NSObject { int _age; NSString *_name; } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; - (int)age; - (void)setAge:(int)age; - (NSString *)name; - (void)setName:(NSString *)name; @end

src8-malan/Students3/Students3/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. // // // // // // // // // //

Student.m Students3 David J. Malan Harvard University [email protected] Defines a student with properties.

#import "Student.h" @implementation Student - (int)age { return _age; } - (void)setAge:(int)age { _age = age; } - (NSString *)name { return _name; } - (void)setName:(NSString *)name { if (_name != name) { [_name release]; _name = [name copy]; } } - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students4/Students4/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. // // // // // // // // // //

Student.h Students4 David J. Malan Harvard University [email protected] Declares a student with synthesized properties.

#import <Foundation/Foundation.h> @interface Student : NSObject { } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; @end

src8-malan/Students4/Students4/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. // // // // // // // // // //

Student.m Students4 David J. Malan Harvard University [email protected] Defines a student with synthesized properties.

#import "Student.h" @implementation Student @synthesize age=_age; @synthesize name=_name; - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students5/Students5/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. // // // // // // // // // //

Student.h Students5 David J. Malan Harvard University [email protected] Declares a student with (mostly) synthesized properties.

#import <Foundation/Foundation.h> @interface Student : NSObject { } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; @end

src8-malan/Students5/Students5/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. // // // // // // // // // //

Student.m Students5 David J. Malan Harvard University [email protected] Defines a student with (mostly) synthesized properties.

#import "Student.h" @implementation Student @synthesize age=_age; @synthesize name=_name; - (void)setName:(NSString *)name { if (_name != name) { [_name release]; if ([name isEqualToString:@"David"]) { _name = [[NSString alloc] initWithString:@"Dummy"]; } else { _name = [name copy]; } } } - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students6/Students6/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. // // // // // // // // // //

Student.h Students6 David J. Malan Harvard University [email protected] Declares a student with init methods.

#import <Foundation/Foundation.h> @interface Student : NSObject { } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; - (id)initWithName:(NSString *)name andAge:(int)age; @end

src8-malan/Students6/Students6/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. // // // // // // // // // //

Student.m Students6 David J. Malan Harvard University [email protected] Defines a student with init methods.

#import "Student.h" @implementation Student @synthesize age=_age; @synthesize name=_name; - (id)init { [self initWithName:@"John" andAge:403]; return self; } - (id)initWithName:(NSString *)name andAge:(int)age { if ((self = [super init])) { self.age = age; self.name = name; } return self; } - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students7/Students7/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. // // // // // // // // // //

Student.h Students7 David J. Malan Harvard University [email protected] Declares a student with init methods.

#import <Foundation/Foundation.h> @interface Student : NSObject { } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; - (id)initWithName:(NSString *)name andAge:(int)age; @end

src8-malan/Students7/Students7/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. // // // // // // // // // //

Student.m Students7 David J. Malan Harvard University [email protected] Defines a student with init methods.

#import "Student.h" @implementation Student @synthesize age=_age; @synthesize name=_name; - (id)init { [self initWithName:@"John" andAge:403]; return self; } - (id)initWithName:(NSString *)name andAge:(int)age { if ((self = [super init])) { self.age = age; self.name = name; } return self; } - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students9/Students9/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. // // // // // // // // // //

Student.h Students9 David J. Malan Harvard University [email protected] Declares a student with convenience method.

#import <Foundation/Foundation.h> @interface Student : NSObject { } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; + (id)studentWithName:(NSString *)name andAge:(int)age; - (id)initWithName:(NSString *)name andAge:(int)age; @end

src8-malan/Students9/Students9/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. // // // // // // // // // //

Student.m Students9 David J. Malan Harvard University [email protected] Defines a student with convenience method.

#import "Student.h" @implementation Student @synthesize age=_age; @synthesize name=_name; + (id)studentWithName:(NSString *)name andAge:(int)age { return [[[Student alloc] initWithName:name andAge:age] autorelease]; } - (id)init { [self initWithName:@"John" andAge:403]; return self; } - (id)initWithName:(NSString *)name andAge:(int)age { if ((self = [super init])) { self.age = age; self.name = name; } return self; } - (void)dealloc { [_name release]; [super dealloc]; } @end

src8-malan/Students10/Students10/Student.h 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. // // // // // // // // // //

Student.h Students10 David J. Malan Harvard University [email protected] Declares a student with (non-init) instance method.

#import <Foundation/Foundation.h> @interface Student : NSObject { } @property (assign, nonatomic, readwrite) int age; @property (copy, nonatomic, readwrite) NSString *name; - (id)initWithName:(NSString *)name andAge:(int)age; - (void)greet; @end

src8-malan/Students10/Students10/Student.m 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. // // // // // // // // // //

Student.m Students9 David J. Malan Harvard University [email protected] Defines a student with (non-init) instance method.

#import "Student.h" @implementation Student @synthesize age=_age; @synthesize name=_name; - (id)init { [self initWithName:@"John" andAge:403]; return self; } - (id)initWithName:(NSString *)name andAge:(int)age { if ((self = [super init])) { self.age = age; self.name = name; } return self; } - (void)greet { NSLog(@"Hello, I am %@. } - (void)dealloc { [_name release]; [super dealloc]; } @end

I am %d years old.\n", _name, _age);

You might also like