Created
January 26, 2011 13:58
-
-
Save rummelonp/796714 to your computer and use it in GitHub Desktop.
Objective-Cでマルチアカウント対応するためのモデル
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 <Foundation/Foundation.h> | |
@interface Account : NSObject | |
{ | |
NSString* uuid; | |
NSString* email; | |
NSString* password; | |
} | |
@property (readonly) NSString* uuid; | |
@property (nonatomic, retain) NSString* email; | |
@property (nonatomic, retain) NSString* password; | |
+ (id)account; | |
- (id)initWithUUID:(NSString*)aUuid; | |
+ (id)accountWithUUID:(NSString*)aUuid; | |
- (void)load; | |
- (void)save; | |
+ (NSString*)createUUID; | |
@end | |
#define KEY_WITH_UUID(key, uuid) ([NSString stringWithFormat:@"%@.%@", uuid, key]) | |
@implementation Account | |
@synthesize uuid; | |
@synthesize email; | |
@synthesize password; | |
- (id)init | |
{ | |
self = [super init]; | |
if (self != nil) { | |
uuid = [[self class] createUUID]; | |
} | |
return self; | |
} | |
+ (id)account | |
{ | |
return [[self alloc] init]; | |
} | |
- (id)initWithUUID:(NSString *)aUuid | |
{ | |
self = [super init]; | |
if (self != nil) { | |
uuid = aUuid; | |
[self load]; | |
} | |
return self; | |
} | |
+ (id)accountWithUUID:(NSString *)aUuid | |
{ | |
return [[self alloc] initWithUUID:aUuid]; | |
} | |
+ (NSString*)createUUID | |
{ | |
CFUUIDRef uuidRef = CFUUIDCreate(nil); | |
NSString* aUuid = (NSString*)CFUUIDCreateString(nil, uuidRef); | |
CFRelease(uuidRef); | |
return aUuid; | |
} | |
- (void)load | |
{ | |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
self.email = [defaults stringForKey:KEY_WITH_UUID(@"email", uuid)]; | |
self.password = [defaults stringForKey:KEY_WITH_UUID(@"password", uuid)]; | |
} | |
- (void)save | |
{ | |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
[defaults setObject:email forKey:KEY_WITH_UUID(@"email", uuid)]; | |
[defaults setObject:password forKey:KEY_WITH_UUID(@"password", uuid)]; | |
} | |
- (BOOL)isEqual:(id)other | |
{ | |
if (other == self) { | |
return YES; | |
} | |
if ([other isKindOfClass:[self class]]) { | |
if ([[other uuid] isEqualToString:uuid]) { | |
return YES; | |
} | |
if ([[other email] isEqualToString:email] && | |
[[other password] isEqualToString:password]) | |
{ | |
return YES; | |
} | |
} | |
return NO; | |
} | |
- (void)dealloc | |
{ | |
if (uuid != nil) { | |
[uuid release]; | |
uuid = nil; | |
} | |
self.email = nil; | |
self.password = nil; | |
[super dealloc]; | |
} | |
@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 <Foundation/Foundation.h> | |
#import "Account.m" | |
@interface Setting : NSObject | |
{ | |
NSString* activeAccountUuid; | |
NSMutableArray* accounts; | |
} | |
@property (nonatomic, retain) NSString* activeAccountUuid; | |
@property (nonatomic, retain) NSMutableArray* accounts; | |
- (void)registerDefaults; | |
- (void)load; | |
- (void)save; | |
- (Account*)activeAccount; | |
@end | |
@implementation Setting | |
@synthesize activeAccountUuid; | |
@synthesize accounts; | |
- (id)init | |
{ | |
self = [super init]; | |
if (self != nil) { | |
self.accounts = [[NSMutableArray alloc] init]; | |
[self registerDefaults]; | |
[self load]; | |
} | |
return self; | |
} | |
- (void)registerDefaults | |
{ | |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
NSArray* keys = [NSArray arrayWithObject:nil]; | |
NSArray* values = [NSArray arrayWithObject:nil]; | |
NSDictionary* settings = [NSDictionary dictionaryWithObjects:values forKeys:keys]; | |
[defaults registerDefaults:settings]; | |
} | |
- (void)load | |
{ | |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
self.activeAccountUuid = [defaults stringForKey:@"defaultAccountUuid"]; | |
NSArray* accountUuids = [defaults arrayForKey:@"accountUuids"]; | |
for (NSString* uuid in accountUuids) { | |
Account* account = [Account accountWithUUID:uuid]; | |
[accounts addObject:account]; | |
} | |
} | |
- (void)save | |
{ | |
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; | |
NSMutableArray* accountUuids = [NSMutableArray arrayWithCapacity:[accounts count]]; | |
for (Account* account in accounts) { | |
[accountUuids addObject:account.uuid]; | |
[account save]; | |
} | |
[defaults setObject:accountUuids forKey:@"accountUuids"]; | |
} | |
- (Account*)activeAccount | |
{ | |
for (Account* account in accounts) { | |
if ([activeAccountUuid isEqualToString:account.uuid]) { | |
return account; | |
} | |
} | |
return nil; | |
} | |
- (void)dealloc | |
{ | |
self.activeAccountUuid = nil; | |
self.accounts = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment