1. Objective C for Samurais by @rafaecheve
10 Ways to Improve
Objective C Code
2.0
Saturday, July 13, 13
2. Objective C for Samurais by @rafaecheve
Understand Objective C
• Objective-C is a superset of C
• Verbosity is a must
• Long names is a must
• It is damn old
Saturday, July 13, 13
3. tip #1
@class PETDog;
Avoid Importing Headers on .h files when possible.
Instead of importing .h files you can use @class to tell the compiler to use
this class.
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
4. NSNumber *intNumber = @3;
NSNumber *floatNumber = @5.5f;
NSNumber *doubleNumber = @3.14;
NSNumber *boolNumber = @YES;
NSNumber *charNumber = @'b';
NSArray *animals = [NSArray arrayWithObjects:@"ocelot", @"lion",
@"tiger", @"donkey", nil];
NSDictionary *geekinfo = @{@"firstName" : @"Rafael", @"lastName" :
@"Echeverria", @"age" : @24};
tip #2 Use Objective 2.0 literals on your declaring needs.
NSString *greeting = @"Awesome Dog";
Since Objective C 2.0 you can declare variables in literal Style.
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
5. tip #3
//.h
extern const NSString ABCDogName;
//.m
const NSString ABCDogName = 0.3;
#define MIDI_DURATION 1.0
Use typed constants and defeat lazyness
Declare constants as typed, and prevend collisions and uncertainty with
created by #define.
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
6. tip #4
enum ABCDogState {
ABCDogStateBarking,
ABCDogStateSleeping,
ABCDogStateEating,
};
switch (_currentState) {
ABCDogStateBarking:
// Handle barking state
break;
ABCDogStateSleeping:
// Handle sleeping state
break;
ABCDogStateEating:
// Handle eating state
break;
}
Enumerate as long you know the status.
Enumerate safely when you know your diferent values for Opcions and
States. it is very common to use it on switch statements.
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
7. tip #5 Take the time to understatang property attributes.
readonly Only a getter is available, and the compiler will generate
it only if the property is synthesized.
readwrite
Both a getter and a setter are available. If the property
is synthesized, the compiler will generate both
methods.
copy This designates an owning relationship similar to strong;
however, instead of retaining the value, it is copied.
unsafe_unretained
This has the same semantics as assign but is used
where the type is an object type to indicate a
nonowning relationship (unretained).
assign The setter is a simple assign operation used for scalar
types, such as CGFloat or NSInteger.
strong
This designates that the property defines an owning
relationship. When a new value is set, it is first
retained, the old value is released, and then the value
is set.
weak This designates that the property defines a nonowning
relationship.
We can assing our properties with diferent properties, as desired.
Saturday, July 13, 13
8. tip #6
NSString *foo = @"Badger 123";
NSString *bar = [NSString stringWithFormat:@"Badger %i", 123];
BOOL equalA = (foo == bar); //< equalA = NO
BOOL equalB = [foo isEqual:bar]; //< equalB = YES
BOOL equalC = [foo isEqualToString:bar]; //< equalC = YES
Objects are equal under diferent circumstances.
Understanting equality is always important to avoid pitfalls at comparing.
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
9. tip #7
@interface ABCDogBreedChihuahua : ABCDog
@end
@implementation ABCDogBreedChihuahua
- (void) doBark {
[self sayHola];
}
@end
Hide your implentation using the Class Cluster Pattern
typedef NS_ENUM(NSUInteger, ABCDogBreed) {
ABCDogBreedChihuahua,
ABCDogBreedLabrador,
};
@interface ABCDog : NSObject
@property (copy) NSString *name;
// Factory Method to create dogs
+ (ABCDog*)dogWithBreed:(ABCDogBreed)type;
- (void)doBark;
@end
@implementation ABCDog
+ (ABCDog*)dogWithBreed:(ABCDogBreed)type{
switch (type) {
case EOCEmployeeTypeDeveloper:
return [ABCDogBreedChihuahua new];
break;
case EOCEmployeeTypeDesigner:
return [ABCDogBreedLabrador new];
break;
}
}
- (void) doBark {
// Subclasses implement this.
}
.h
.m
subclass
This can save tons of lines and helps to keep things DRY
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
10. tip #8
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict isMemberOfClass:[NSDictionary class]]; ///< NO
[dict isMemberOfClass:[NSMutableDictionary class]]; ///< YES
[dict isKindOfClass:[NSDictionary class]]; ///< YES
[dict isKindOfClass:[NSArray class]]; ///< NO
Use Inspection to reveal class secrets.
Understanding how to inspect a class is a powerful tecnique
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
11. tip #9 Use Prefix to avoid class clashes.
Use the name of your app, or company name is up to you.
ABCDog
ABCCat
ABCDonkey
com.bigco.myapp
BCOPerson
com.abcinc.myapp
BCOJob
BCOAccount
Objective C for Samurais by @rafaecheve
Saturday, July 13, 13
12. tip #10
- (id)initWithName:(NSString *)name
andAge:(NSNumber)age {
if ((self = [super init])) {
_name = name;
_age = age;
}
return self;
}
Customize you initializers as needed
This allows you to create you objects easly using what you want.
Objective C for Samurais by @rafaecheve
- (id)initWithName:(NSString *)name
andAge:(NSNumber)age
Saturday, July 13, 13