单例模式-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);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。