开发者

Problem with singleton

开发者 https://www.devze.com 2023-02-21 01:29 出处:网络
I want to make a singleton containing information \"title, comments, Two picture\" and it saves all the information in an array

I want to make a singleton containing information "title, comments, Two picture" and it saves all the information in an array I want to do is these objects in my application I use it All The Time

@interface CarteManager : NSObject {


NS开发者_开发技巧MutableArray *carteMan ; 

}

@property(nonatomic,retain) NSMutableArray *carteMan; 

+(CarteManager*)sharedInstance;

-(void)ajouttitre:(NSString*)txt; 
-(void)ajoutcom:(NSString*)com; 
-(void)ajoutimage1:(UIImage*)img; 
-(void)ajoutimage2:(UIImage*)img; 


@end


In order to create a Singleton you will need a static instance.

@implementation CarteManager

static CarteManager *_carteManager = nil;

+(CarteManager*)sharedInstance {
   if (!_carteManager) {
        _carteManager = [[CarteManager alloc] init];
    }

    return _carteManager;
}

// your other codes

@end

And before creating a Singleton, make sure that you really need a Singleton. Please pay special attention to Singleton: How should it be used.


You didn't state your problem. If it's how to make the object a singleton, you can find several possible implementations in the question What does your Objective-C singleton look like?.

0

精彩评论

暂无评论...
验证码 换一张
取 消