IOS--单例类
2015-10-21 17:17
435 查看
单例类就是要把创建的类别中的方法共享给其他类别
//—————-***—******ViewController.m
#import "ViewController.h"
#import "Singleton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
shareSingleton=[[Singleton alloc]init];
[[Singleton sharedsingleton]wcwf];
}
@end
//
// Singleton.m
// 单例
//
// Created by Mac on 15/10/15.
// Copyright (c) 2015年 MaYang. All rights reserved.
//
#import "Singleton.h"
static Singleton*sharedsingleton;
@implementation Singleton
+(Singleton*)sharedsingleton
{
if (sharedsingleton==nil)
{
sharedsingleton=[[Singleton alloc]init];
}
return sharedsingleton;
}
-(void)wcwf
{
int a=1;
int b=2;
int c=a+b;
NSLog(@"%d",c);
}
// Singleton.h
// Copyright (c) 2015年 MaYang. All rights reserved.
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
+(Singleton*)sharedsingleton;
-(void)wcwf;
@end
@end
//—————-***—******ViewController.m
#import "ViewController.h"
#import "Singleton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
shareSingleton=[[Singleton alloc]init];
[[Singleton sharedsingleton]wcwf];
}
@end
//
// Singleton.m
// 单例
//
// Created by Mac on 15/10/15.
// Copyright (c) 2015年 MaYang. All rights reserved.
//
#import "Singleton.h"
static Singleton*sharedsingleton;
@implementation Singleton
+(Singleton*)sharedsingleton
{
if (sharedsingleton==nil)
{
sharedsingleton=[[Singleton alloc]init];
}
return sharedsingleton;
}
-(void)wcwf
{
int a=1;
int b=2;
int c=a+b;
NSLog(@"%d",c);
}
// Singleton.h
// Copyright (c) 2015年 MaYang. All rights reserved.
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
+(Singleton*)sharedsingleton;
-(void)wcwf;
@end
@end
相关文章推荐
- IOS----断点下载
- iOS开发中的一些细节
- iOS每日一记-----之 简单的按钮切换
- IOS 开源库介绍
- iOS中定时器NSTimer的使用
- IOS添加计时器的两种方法
- iOS 内存管理(strong weak copy)详解
- fir.im Weekly - 热门 iOS 第三方库大盘点
- 有时间整理一下 iOS后台计时器。
- iOS 精益编程
- IOS 世界标准时间UTC /GMT 转为当前系统时区对应的时间
- 【iOS】GitHub删除respository(仓库)、fork项目
- fir.im Weekly - 热门 iOS 第三方库大盘点
- IDFA - 2
- IOS9 解决中文乱码
- ios Autolayout改变约束, 动画效果处理
- iOS常用字符串使用
- iOS中的中间人-NSURLProtocol 的原理和使用实例
- iOS中GIF的制作的简单版本记录
- iOS:GIF图片的预览以及生成