GSLSynthesizeSingleton is a simple macro to synthesize the methods for an Objective-C singleton class.
- Xcode 13.0 or later
- Base SDK: iOS 15 / macOS 11.3 / watchOS 8 / tvOS 15 or later
- Deployment Target: iOS 6.0 / OS X 10.6 / watchOS 2.0 / tvOS 9.0 or later
Add the pod to your Podfile
:
# ...
pod 'GSLSynthesizeSingleton'
Install the pod:
$ pod install
Check out the source:
$ git clone https://github.com/GyazSquare/GSLSynthesizeSingleton.git
Copy GSLSynthesizeSingleton.h into your project.
- Declare a singleton class property or method in your header file.
- Add one macro in your source file.
XYZManager.h
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface XYZManager : NSObject
+ (instancetype)sharedManager;
// ...
@end
NS_ASSUME_NONNULL_END
Or if you use the class from Swift, you'd better declare as follows:
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface XYZManager : NSObject
@property (class, readonly) XYZManager *sharedManager;
- (instancetype)init NS_DESIGNATED_INITIALIZER OBJC_SWIFT_UNAVAILABLE("use 'aClass.shared' instead");
// ...
@end
NS_ASSUME_NONNULL_END
XYZManager.m
#import "GSLSynthesizeSingleton.h"
#import "XYZManager.h"
@implementation XYZManager
GSLSynthesizeSingleton(XYZ, Manager);
- (instancetype)init {
self = [super init];
if (self) {
// ...
}
return self;
}
// ...
@end
This software is licensed under the MIT License.
See the LICENSE file for details.