Skip to content

Commit ada325d

Browse files
committed
Move hardcoded keys array to User class
1 parent cec7243 commit ada325d

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

MyAuth/MyAuth/MasterViewController.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#import <UIKit/UIKit.h>
22

33
@interface MasterViewController : UITableViewController
4-
54
@end

MyAuth/MyAuth/MasterViewController.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
@interface MasterViewController () {
1212
NSDictionary *_menu;
13-
NSArray *_keys;
1413
}
1514
@end
1615

@@ -20,8 +19,6 @@ - (void)awakeFromNib
2019
{
2120
[super awakeFromNib];
2221

23-
_keys = @[kFB, kGG, kMR, kOK, kVK];
24-
2522
_menu = @{
2623
kFB: @{
2724
kKey: kFB,
@@ -71,14 +68,14 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
7168

7269
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
7370
{
74-
return _keys.count;
71+
return [[User keys] count];
7572
}
7673

7774
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
7875
{
7976
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
8077

81-
NSString *key = _keys[indexPath.row];
78+
NSString *key = [User keys][indexPath.row];
8279
NSString *label = _menu[key][kLabel];
8380
cell.textLabel.text = label;
8481
return cell;
@@ -87,7 +84,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
8784
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
8885
{
8986
[tableView deselectRowAtIndexPath:indexPath animated:NO];
90-
NSString *key = _keys[indexPath.row];
87+
NSString *key = [User keys][indexPath.row];
9188
NSLog(@"%s: key=%@", __PRETTY_FUNCTION__, key);
9289
[User saveDefaultKey:key];
9390
User* user = [User loadForKey:key];

MyAuth/MyAuth/User.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
-(void)save;
1414
-(void)reset;
1515

16+
+(NSArray*)keys;
1617
+(User*)load;
1718
+(User*)loadForKey:(NSString*)key;
1819
+(void)saveDefaultKey:(NSString*)key;

MyAuth/MyAuth/User.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#import "User.h"
2+
#import "SocialNetwork.h"
23

3-
static NSString* const kKey = @"key";
44
static NSString* const kUserId = @"userId";
55
static NSString* const kFirstName = @"firstName";
66
static NSString* const kLastName = @"lastName";
@@ -52,11 +52,23 @@ -(void)reset
5252
[defaults synchronize];
5353
}
5454

55+
+(NSArray*)keys
56+
{
57+
static NSArray *_keys;
58+
static dispatch_once_t token;
59+
60+
dispatch_once(&token, ^{
61+
_keys = @[kFB, kGG, kMR, kOK, kVK];
62+
});
63+
64+
return _keys;
65+
}
66+
5567
+(User*)load
5668
{
5769
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
5870
NSString *key = [defaults objectForKey:kKey];
59-
if (!key)
71+
if ([[User keys] containsObject:key] == NO)
6072
return nil;
6173

6274
NSData *archived = [defaults objectForKey:key];
@@ -70,7 +82,7 @@ +(User*)load
7082
+(User*)loadForKey:(NSString*)key
7183
{
7284
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
73-
if (!key)
85+
if ([[User keys] containsObject:key] == NO)
7486
return nil;
7587

7688
NSData *archived = [defaults objectForKey:key];

0 commit comments

Comments
 (0)