Jason1923
Jason1923

Reputation: 93

Forcefully initialize class with unavailable init in Swift

The SCWindow class has all its initializers marked as unavailable. In Objective-C, I can get around it using class extensions:

#import <ScreenCaptureKit/ScreenCaptureKit.h>

@interface SCWindow (InitWithId)

@property CGWindowID windowID;

- (instancetype _Nonnull)initWithId:(CGWindowID)windowID;

@end

@implementation SCWindow (InitWithId)

@dynamic windowID;

- (instancetype _Nonnull)initWithId:(CGWindowID)windowID {
    self = [super init];
    if (self) {
        [self setValue:[NSNumber numberWithInteger:windowID] forKey:@"windowID"];
    }
    return self;
}

@end

Is there a technique to do this in Swift? I believe Swift extensions only allow convenience initializers, not designated ones like Objective-C.

Upvotes: 0

Views: 219

Answers (0)

Related Questions