小编kns*_*shn的帖子

如何声明x和y以便x + = y给出编译错误而x = x + y不?

我在接受采访时遇到了这个问题,无法提出解决方案.我知道反之亦然,如"+ ="运算符在Java中的作用所示?

所以问题如下.

..... x = .....;
..... y = .....;

x += y; //compile error
x = x + y; //works properly
Run Code Online (Sandbox Code Playgroud)

java operators shorthand

55
推荐指数
1
解决办法
7297
查看次数

Singleton和init带参数

我想在我的类中使用具有private initwith参数的单例模式.它还有一个类函数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)

swift

22
推荐指数
3
解决办法
2万
查看次数

标签 统计

java ×1

operators ×1

shorthand ×1

swift ×1