Created
December 23, 2013 15:15
-
-
Save modocache/8098767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Car.h | |
// | |
#import <Foundation/Foundation.h> | |
@interface Car : NSObject | |
@property (nonatomic, assign) NSInteger horsepower; | |
+ (Car *)car; | |
- (void)vroom; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Car.m | |
// | |
#import "Car.h" | |
// C method. It returns nothing--`void`. | |
// Within these methods we can call public | |
// methods declared on the `Car` class. | |
void makeCarGoVroom(Car *car) { | |
[car vroom]; | |
} | |
// Another C method. | |
void makeCarThatThenGoesVroom() { | |
Car *car = [Car car]; | |
makeCarGoVroom(car); | |
} | |
@implementation Car | |
// Instance method. | |
- (void)vroom { | |
// I can access instance variables such as _horsepower. | |
// Here, `self` refers to this object, an instance of the `Car` class. | |
for (int i = 0; i < self.horsepower; i++) { | |
NSLog(@"Vroom!"); | |
} | |
} | |
// Class method. | |
+ (id)car { | |
// Cannot access instance variables such as _horsepower. | |
// Here, `self` refers to the `Car` class. | |
return [[self alloc] init]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment