Wesley
Wesley

Reputation: 5621

Consume a swift defined convenience init in an objective c method

I have a swift extension to NSAttributedString defined in a swift package as below:

@objc public extension NSAttributedString {
    convenience init?(html: String) {
        //Do custom clean up
    }
}

I have successfully imported the package and imported the module into my Objective-C class:

@import MySwiftPackage;

However, when I go to create a new NSAttributedString, the I get an No visible @interface for 'NSAttributedString' declares the selector 'html' error:

NSAttributedString *test = [[NSAttributedString alloc] html]

Where am I going wrong on this one?

Upvotes: 0

Views: 104

Answers (1)

alexander.cpp
alexander.cpp

Reputation: 838

Objective-C compiler expects your initializer to be called as initWithHtml: (as @Larme suggested in comments).

This should work:

NSString *myHtmlString = @"String you want to pass as 'html' parameter to your init";
NSAttributedString *test = [[NSAttributedString alloc] initWithHtml:myHtmlString];

Your extension looks good, because it's marked as @objc and public, so it doesn't need any changes.

Upvotes: 1

Related Questions