单例类就是要把创建的类别中的方法共享给其他类别
//—————-***—******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