Classes that use Singleton mode:
UIApplication
Uiaccelerometer
Nsuserdefaults
Nsnotificationcenter
Nsfilemanager
NSBundle, etc.
Singleton.h
#import <Foundation/Foundation.h>@interface singleton:nsobject// Always return the same Singleton pointer + (Singleton **singletondata; @end
Singleton.m
#import "Singleton.h"@implementationSingleton@synthesizeSingletondata =_singletondata;StaticSingleton *sharemanger =Nil;/** This method uses the GCD (Grand Central Dispatch) technology, which is an opportunity for the C-language multi-threaded access count. The Dispatch_once function is provided by GCD, and its purpose is to execute only one block of code throughout the application life cycle. Dispatch_once_t is a struct provided by GCD, which is used to pass the GCD address to the Dispatch_once function. The Dispatch_once function can record whether the code is being called quickly or not. **/+ (Singleton *) sharedmanager{Staticdispatch_once_t once; Dispatch_once (&once, ^{Sharemanger=[[Self alloc] init]; }); returnSharemanger;}@end
Singleappdelegate
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ @" Singleton Init " ; *bundle = [NSBundle mainbundle]; NSLog (@ "bundle =%@", bundle); return YES;}
Singleviewcontroller.m
-(void) viewdidload{ [Super Viewdidload]; NSLog (@ "viewdidload Print:%@", [Singleton sharedmanager].singletondata); *bundle = [NSBundle mainbundle]; NSLog (@ "bundle =%@", bundle);}