forked from thoughtbot/guides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectiveC.m
71 lines (52 loc) · 1.95 KB
/
ObjectiveC.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#import "Alpha.h"
#import "Beta.h"
// Use @interface extensions for private properties
@interface ClassName () <Protocols>
// Keep @properties grouped together by function
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic, readonly) TBObject *someObject;
@end
// Use static NSString points to consts for string constants
static NSString *const ConstantName = @"Constant";
// Prepend constants with 'k' when being used as keys
static NSString *const kFirstName = @"FirstName";
@implementation ClassName
/*
- Use #pragma mark to organize code by function
- Use descriptive names for #pragma mark
- Use class names if overriding or implementing protocol methods
*/
#pragma mark - Initialization
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
// Return early if conditions prohibit the intended function of the method
// Use conditionals for exceptional cases
// Keep the 'optimal' path non-indented
if (!self)
return nil;
return self;
}
#pragma mark - UI
// Opening brackets belong on the next line
- (void)shuffleCards
{
// Objective-C literals are your friend
NSDictionary *themeColors = @{ kRedColor : [UIColor redColor], kBlueColor : [UIColor blueColor] };
NSArray *robots = @[ @"Ralph", @"Bender", @"The Iron Giant" ];
NSMutableArray *deckOfCards = [NSMutableArray arrayWithCapacity:52];
// Newlines before and after conditional blocks
for (Card *card in deckOfCards)
NSLog(@"%@", [card description]);
Card *jokerCard = [Card joker];
[deckOfCards addObject:jokerCard];
// Use ! to check for nots. Comparing to 'nil' is redundant
if (![creditCard isValid])
{
//...
}
}
@end