Skip to content

Instantly share code, notes, and snippets.

@ykws
Created January 21, 2020 03:20
Show Gist options
  • Save ykws/38f18bee09729992c18e87166fcfc29b to your computer and use it in GitHub Desktop.
Save ykws/38f18bee09729992c18e87166fcfc29b to your computer and use it in GitHub Desktop.
SharedManager by Objective-C
@implementation AnyManager
+ (instancetype)sharedManager {
static AnyManager *_sharedManager = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedManager = [[self alloc] init];
});
return _sharedManager;
}
@end
@ykws
Copy link
Author

ykws commented Jan 21, 2020

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