I'm creating a compatibility layer for a new Swift implementation of a legacy Objective-C library. The Objective-C library has a class whose only constructor is a factory constructor:
I know that when bridging from Objective-C to Swift the factory constructors are turned into convenience initializers. But I'm trying to do the reverse, that is create a factory constructor for a Swift class. The convenience initializer gets turned into something along the lines of:
So my approach is to create a static method in Swift that returns an instance of Self:
This gets exposed correctly, but I'm a little uneasy since it's clearly asymmetric with the Objective-C -> Swift bridging, and also it returns an owned reference, where the Objective-C convention would be that a factory constructor returns an autoreleased object.
Long story short, is the static func the way to go, and am I creating a memory leak here? (and if so, how should I fix it?)
Code Block + (instancetype)fooWithBar:(NSString *)bar;
I know that when bridging from Objective-C to Swift the factory constructors are turned into convenience initializers. But I'm trying to do the reverse, that is create a factory constructor for a Swift class. The convenience initializer gets turned into something along the lines of:
Code Block - (instancetype)initWithBar:(NSString *)bar;
So my approach is to create a static method in Swift that returns an instance of Self:
Code Block @objc static func foo(bar: String) -> Self
This gets exposed correctly, but I'm a little uneasy since it's clearly asymmetric with the Objective-C -> Swift bridging, and also it returns an owned reference, where the Objective-C convention would be that a factory constructor returns an autoreleased object.
Long story short, is the static func the way to go, and am I creating a memory leak here? (and if so, how should I fix it?)