Skip to content

Instantly share code, notes, and snippets.

@modocache
Created December 23, 2013 15:15
Show Gist options
  • Save modocache/8098767 to your computer and use it in GitHub Desktop.
Save modocache/8098767 to your computer and use it in GitHub Desktop.
//
// Car.h
//
#import <Foundation/Foundation.h>
@interface Car : NSObject
@property (nonatomic, assign) NSInteger horsepower;
+ (Car *)car;
- (void)vroom;
@end
//
// 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