Last active
September 5, 2015 12:34
-
-
Save benlodotcom/492234 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
@interface MyClass : NSObject { | |
iVar *aProtectedIVar; | |
@public | |
iVar *aPublicIVar; | |
iVar *aSecondPublicIVar; | |
@protected | |
iVar *aSecondProtectedIVar; | |
@private | |
iVar *aPrivateIVAr; | |
} | |
@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
/*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 |
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
/*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 |
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
@interface MyClass : NSObject { | |
//iVars declaration | |
} | |
/*We declare the property as readonly in the header*/ | |
@property (readonly, nonatomic, retain) Type *aProperty; | |
@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
@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 |
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
@interface TheMechanics : NSObject { | |
@private | |
NSArray *_tools; | |
} | |
- (void)repairCar:(Car *)aCar; | |
@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
#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 |
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
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