Skip to content

Instantly share code, notes, and snippets.

@benlodotcom
Last active September 5, 2015 12:34
Show Gist options
  • Save benlodotcom/492234 to your computer and use it in GitHub Desktop.
Save benlodotcom/492234 to your computer and use it in GitHub Desktop.
@interface MyClass : NSObject {
iVar *aProtectedIVar;
@public
iVar *aPublicIVar;
iVar *aSecondPublicIVar;
@protected
iVar *aSecondProtectedIVar;
@private
iVar *aPrivateIVAr;
}
@end
/*We declare the category*/
@interface MyClass (private)
@property (nonatomic, retain) Type *aPrivateProperty;
- (void)aPrivateMethod;
@end
/*We implement it*/
@implementation MyClass (private)
/*We cannot use synthesize in the category :-(*/
@dynamic aPrivateProperty;
- (Type *)aPrivateProperty {
//Getter code
}
- (void)setAPrivateProperty {
//Setter code
}
- (void)aPrivateMethod {
//Some code there
}
@end
/*We declare the class extension*/
@interface MyClass ()
@property (nonatomic, retain) Type *aPrivateProperty;
- (void)aPrivateMethod;
@end
/*
We can use the main implementation block to implement our properties
and methods declared in the class extension.
*/
@implementation MyClass
/*Therefore we can use synthesize ;-)*/
@synthesize aPrivateProperty;
- (void)aPrivateMethod {
//Some code there
}
@end
@interface MyClass : NSObject {
//iVars declaration
}
/*We declare the property as readonly in the header*/
@property (readonly, nonatomic, retain) Type *aProperty;
@end
@interface MyClass ()
/*
We redeclare the property in the .m file but this time with readwrite access.
When accessed from inside an instance of this class you can set the value of the
property, when accessed from outside you can only read its value.
*/
@property (readwrite, nonatomic, retain) Type *aProperty;
@end
@interface TheMechanics : NSObject {
@private
NSArray *_tools;
}
- (void)repairCar:(Car *)aCar;
@end
#import "TheMechanics.h"
@interface TheMechanics ()
- (void)useToolOnCar:(Car *)aCar;
@property (nonatomic, retain) NSArray *tools;
@end
@implementation TheMechanics
@synthesize tools = _tools;
- (void)repairCar:(Car *)aCar {
for (Tool *tool in self.tools) {
[self useToolOnCar:aCar]
}
}
@end
TheMechanics *mechanics = [[TheMechanics alloc] init];
[mechanics repairCar:self.car];
[self lookingBack:OverMyShoulder];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment