我在接受采访时遇到了这个问题,无法提出解决方案.我知道反之亦然,如"+ ="运算符在Java中的作用所示?
所以问题如下.
..... x = .....;
..... y = .....;
x += y; //compile error
x = x + y; //works properly
Run Code Online (Sandbox Code Playgroud) 我想在我的类中使用具有private init
with参数的单例模式.它还有一个类函数setup
,用于配置和创建共享实例.我的Objective-c代码是:
@interface MySingleton: NSObject
+ (MySingleton *)setup:(MyConfig *)config;
+ (MySingleton *)shared;
@property (readonly, strong, nonatomic) MyConfig *config;
@end
@implementation MySingleton
static MySingleton *sharedInstance = nil;
+ (MySingleton *)setup:(MyConfig *)config {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] initWithConfig:config];
});
// Some other stuff here
return sharedInstance;
}
+ (MySingleton *)shared {
if (sharedInstance == nil) {
NSLog(@"error: shared called before setup");
}
return sharedInstance;
}
- (instancetype)initWithConfig:(RVConfig *)config {
self = [super init];
if …
Run Code Online (Sandbox Code Playgroud)