代码-Manager.h文件:
#import <Foundation/Foundation.h> @interface Manager : NSObject @property (nonatomic, strong, class, readonly) Manager *sharedManager; @property (nonatomic, copy, class) NSString *name; @end
代码-Manager.m文件:
#import "Manager.h" static NSString *_name = nil; @implementation Manager + (Manager *)sharedManager { static Manager *_instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } + (void)setName:(NSString *)name { _name = [name copy]; } + (NSString *)name { return _name; } @end
输出:
manager1 = <Manager: 0x100100240>, manager2 = <Manager: 0x100100240> Manager.name = Jack
Github: