你的位置:首页 > 软件开发 > 操作系统 > 【代码笔记】单例

【代码笔记】单例

发布时间:2015-12-25 10:00:12
一,工程图。二,代码。NetManager.h#import <Foundation/Foundation.h>@interface NetManager : NSObject+ (id)sharedManager;-(void)firstPrintf;-(void) ...

【代码笔记】单例

一,工程图。

【代码笔记】单例

二,代码。

NetManager.h

【代码笔记】单例
#import "NetManager.h"static NetManager *manager;@implementation NetManager#pragma mark - 获取单例+ (id)sharedManager{  if (!manager) {    manager = [[NetManager alloc]init];  }  return manager;}-(void)firstPrintf{  NSLog(@"first Printf!!!!");}-(void)secondPrintf{  NSLog(@"second printf!!!!!");}-(void)threeprintf{  NSLog(@"three printf!!!!!!!");}-(void)fourprintf{  NSLog(@"fourprintf!!!!!!");}
【代码笔记】单例

 

RootViewController.h

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController@end
#import "RootViewController.h"//加上单例的头文件#import "NetManager.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  if (self) {    // Custom initialization  }  return self;}- (void)viewDidLoad{  [super viewDidLoad];  // Do any additional setup after loading the view.      //单例的用法,单例中的函数,可以在程序中直接使用。  [[NetManager sharedManager] firstPrintf];  [[NetManager sharedManager] secondPrintf];  [[NetManager sharedManager] threeprintf];  [[NetManager sharedManager] fourprintf];}- (void)didReceiveMemoryWarning{  [super didReceiveMemoryWarning];  // Dispose of any resources that can be recreated.}
【代码笔记】单例

 

三,输出。

2015-10-13 13:55:17.551 单例[7675:197760] first Printf!!!!2015-10-13 13:55:17.551 单例[7675:197760] second printf!!!!!2015-10-13 13:55:17.551 单例[7675:197760] three printf!!!!!!!2015-10-13 13:55:17.551 单例[7675:197760] fourprintf!!!!!!

 

 


 

原标题:【代码笔记】单例

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录