I'm getting an "ambiguous use of init" error when building with Xcode 7.3 and Swift 2.2
The issue is related to two Objective-C classes and how Swift views their initializers.
Objc sees:
Superclass
@interface Foo: NSManagedObject
+(instancetype)fooWithOwner:(Owner *)owner insertIntoManagedObjectContext:(NSManagedObjectContext *)context;
Subclass
@interface Bar: Foo
+(instancetype)barWithOwner:(Owner *)owner insertIntoManagedObjectContext:(NSManagedObjectContext *)context;
while Swift only sees:
init(owner: Owner!, insertIntoManagedObjectContext context: NSManagedObjectContext!)
which is causing the compiler to be unsure of which init is being called on the subclass "Bar". Is there a way to specify which initializer to use when initializing the subclass? I'd like to avoid refactoring the init method if possible.
It is due to ObjC->Swift API translation rules.
You can use swift_name
attribute to overwrite these rules.
@interface Foo: NSManagedObject
+(instancetype)fooWithOwner:(Owner *)owner insertIntoManagedObjectContext:(NSManagedObjectContext *)context NS_SWIFT_NAME(foo(owner:moc:));
@interface Bar: Foo
+(instancetype)barWithOwner:(Owner *)owner insertIntoManagedObjectContext:(NSManagedObjectContext *)context NS_SWIFT_NAME(bar(owner:moc:));
Then you can call them separately in Swift code:
let foo = Bar.foo(owner: owner, moc: context)
let bar = Bar.bar(owner: owner, moc: context)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加