您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: