【代码笔记】单例
2015-12-25 09:18
190 查看
一,工程图。
二,代码。
NetManager.h
NetManager.m
RootViewController.h
RootViewController.m
三,输出。
二,代码。
NetManager.h
#import <Foundation/Foundation.h> @interface NetManager : NSObject + (id)sharedManager; -(void)firstPrintf; -(void)secondPrintf; -(void)threeprintf; -(void)fourprintf; @end
NetManager.m
#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
RootViewController.m
#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!!!!!!
相关文章推荐
- PHPCMS V9 学习总结
- pyqt QIcon 使用
- 在eclipse中调试服务器中的项目
- 【代码笔记】单例
- Java文件操作系列[1]——PDFBox实现分页提取PDF文本
- Java线程(一):线程安全与不安全
- Java高级部分容器重点总结上
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
- 排序算法—希尔排序算法分析与实现(Python)
- Java web项目中java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- php实现统计目录文件大小的函数
- numpy转换
- 详解Java的JDBC中Statement与PreparedStatement对象
- SAS学习笔记之《SAS编程与数据挖掘商业案例》(1)系统简介和编程基础
- SAS学习笔记之《SAS编程与数据挖掘商业案例》(1)系统简介和编程基础
- Think in Java(一):Java基础
- C语言string.h中常用字符函数介绍
- Python教程
- 17个新手常见Python运行时错误
- Python异常