Created
January 21, 2020 03:20
-
-
Save ykws/38f18bee09729992c18e87166fcfc29b to your computer and use it in GitHub Desktop.
SharedManager by Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation AnyManager | |
+ (instancetype)sharedManager { | |
static AnyManager *_sharedManager = nil; | |
static dispatch_once_t oncePredicate; | |
dispatch_once(&oncePredicate, ^{ | |
_sharedManager = [[self alloc] init]; | |
}); | |
return _sharedManager; | |
} | |
@end |
https://www.toyship.org/archives/1770
これは簡易版であり、 alloc/copy を呼び出し側で使わない注意が必要。
allocWithZone
copyWithZone
を実装することで Singleton の実装になる。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
References
https://github.com/AFNetworking/AFNetworking/blob/555385d2b8c6eb736fc2f5e3aeac46ec2d5ffddd/AFNetworking/AFNetworkReachabilityManager.m#L116-L126