본문 바로가기

Program/iOS

Object-C 전역객체 (shard Object)

현업에서 알게되는 정보를 모두 여기에 정리를 하려고 했는데...막상 현업이 빡세서...엄청 안 되네요.

아이폰 어플 3번째 개발 중이네요..

회사에서 프로젝트로 몇 번 하다보니...일정 압박으로 계속 고민하고 있었지만....찾아보지 못 했던..

전역객체 사용을 이번에 찾아봤습니다.

의외로 생각했던 구조와 비슷해서..프로젝트를 여러 번 하니 이제 감이 좀 오나..이런 생각이 건방지게 

들더군요.ㅋ...지송지송.

암튼 각설하고..소스를 보죠.

In iPhone development, if/when you need a single instance of a variable that can be shared and manipulated where and how should you implement this. Thus far I have found 2 ways of doing so. The first would be to add the global variables to your AppDelegate (which can be done and will be explained but isn’t the preferred method). The second and ‘correct’ way of going about globals is to create a Singleton.

For the following examples we will assume the global variable you are trying to implement is a User object.

User.h

#import <Foundation/Foundation.h>

@interface User : NSObject {

NSString *username;

NSString *password;

NSString *accountType;

}

@property (nonatomicretain) NSString *username;

@property (nonatomicretain) NSString *password;

@property (nonatomicretain) NSString *accountType;

User.m

#import “User.h”

@implementation User

@synthesize username;

@synthesize password;

@synthesize accountType;


App Delegate Method

ApplicationAppDelegate.h

#import “User.h”


@interface ApplicationAppDelegate : NSObject <UIApplicationDelegate> {

NSManagedObjectModel *managedObjectModel;

NSManagedObjectContext *managedObjectContext;

NSPersistentStoreCoordinator *persistentStoreCoordinator;

UIWindow *window;

User *user;

}

@property (nonatomicretainreadonly) NSManagedObjectModel *managedObjectModel;

@property (nonatomicretainreadonly) NSManagedObjectContext *managedObjectContext;

@property (nonatomicretainreadonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (nonatomicretainIBOutlet UIWindow *window;

@property (nonatomicretain) User *user;


ApplicationAppDelegate.m

#import “ApplicationAppDelegate.h”

@implementation ApplicationAppDelegate

@synthesize window;

@synthesize user;

#pragma mark -

#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window addSubview:tabBarController.view];

// Override point for customization after app launch

[window makeKeyAndVisible];

user = [[User allocinit];

}

Then from any view controller you can access the Delegate variable as such.

SomeViewController.m

UIApplication *app = [UIApplication sharedApplication];

app.user.username = @”Username”;

While this can be done and will work. The overall use of the AppDelegate for this functionality is incorrect. The AppDelegate should be used for the following 2 reasons:

  • implemenations of the NSApplication delegate methods (includingapplicationDidFinishLaunching: to finalize application construction)
  • handling menu items for items that don’t exist in a window (for example, opening the application Preferences window)

So, on that note, lets implement a Singleton. The ‘correct’ way of creating a single global instance of an object that can be accessed and manipulated by the view controllers.


Singleton Method

First we need to make some changes to our User class as such:

User.h

#import <Foundation/Foundation.h>

@interface User : NSObject {

NSString *username;

NSString *password;

NSString *accountType;

}

@property (nonatomicretain) NSString *username;

@property (nonatomicretain) NSString *password;

@property (nonatomicretain) NSString *accountType;

+ (User *)sharedUser;

@end

User.m

#import “User.h”

static User *sharedUser = nil;

@implementation User

@synthesize username;

@synthesize password;

@synthesize accountType;

#pragma mark -

#pragma mark Singleton Methods

+ (User *)sharedUser {

if(sharedUser == nil){

sharedUser = [[super allocWithZone:NULLinit];

}

return sharedUser;

}

+ (id)allocWithZone:(NSZone *)zone {

return [[self sharedManagerretain];

}

- (id)copyWithZone:(NSZone *)zone {

return self;

}

- (id)retain {

return self;

}

- (unsigned)retainCount {

return NSUIntegerMax;

}

- (void)release {

//do nothing

}

- (id)autorelease {

return self;

}

@end

Now, in any view controller we need to access/manipulate the ‘global’ user we can do:

SomeViewController.m

#import “SomeViewController.h”

#import “User.h”

@implementation SomeViewController

- (void)someFunction{

User *user = [User sharedUser];

user.username = @”Username”;

}

@end

'Program > iOS' 카테고리의 다른 글

Mac - Parallels 한영전환  (1) 2010.12.24
iPhone Safari  (0) 2010.11.06
Provisioning profile 강제 갱신!!  (1) 2010.08.30
PhotoShop cs Mac  (3) 2010.07.30
Mac MS Office  (0) 2010.07.30