单例模式-ios

#import <Foundation/Foundation.h>


@interface UserContext : NSObject <NSCopying>

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *emial;

+(id) shareUsercontext;

@end

 

#import "UserContext.h"

static UserContext *segtonInstance=nil;

@implementation UserContext

+(id) shareUsercontext{
    
    @synchronized(self){
        
    if(segtonInstance==nil){
        segtonInstance=[[[self class] alloc] init];
        }
        
    }
    return segtonInstance;
}

+(id) allocWithZone:(struct _NSZone *)zone{
    if (segtonInstance==nil) {
        segtonInstance=[super allocWithZone:zone];
    }
    return segtonInstance;
}

- (id)copyWithZone:(NSZone *)zone{
    
    return segtonInstance;
    
}

@end

 

/**单例模式***/
        UserContext *usercontext=[UserContext shareUsercontext];
        UserContext *usercontext2=[UserContext shareUsercontext];
        UserContext *usercontext3=[[UserContext alloc] init];
        UserContext *usercontext4=[usercontext copy];
        NSLog(@"%p",usercontext);
        NSLog(@"%p",usercontext2);
        NSLog(@"%p",usercontext3);
        NSLog(@"%p",usercontext4);

 

单例模式-ios,布布扣,bubuko.com

单例模式-ios

上一篇:UVA - 12002 Happy Birthday


下一篇:ios 图片裁剪