「ios開発」iosのシングルトン(Singleton)のサンプル

サンプルコード
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
@property (strong, nonatomic) NSString *oops1;
@property (strong, nonatomic) NSString *oops2;
+(Singleton*)ShareData;
@end

****************************
#import “Singleton.h"
@implementation Singleton
static Singleton *singletonData = nil;
+(Singleton*)ShareData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singletonData=[[Singleton alloc]init];
});
return singletonData;
}
-(id)init
{
if(self=[super init])
{
self.oops1=@"";
self.oops2=@"";
}

return self;
}
@end

使い方:

Singleton * s1=[Singleton ShareData];
s1.oops1=@"1″;
s1.oops2=@"2″;

IOS

Posted by arkgame