user3459799
user3459799

Reputation: 355

"Cannot Find Initializer" when using Objective-C class in Swift

First time creating a swift app and it was going well until I ran into an issue when trying to use an Objective-C class into the Swift ViewController.

Objective-C Class Init (RKDropdownAlert.h)

+(void)title:(NSString*)title message:(NSString*)message backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor time:(NSInteger)seconds;

My attempt:

var alert: RKDropdownAlert = RKDropdownAlert(title:"Error", message:"Please enter valid credentials.", backgroundColor:UIColor.redColor(), textColor:UIColor.whiteColor(), time:3)

Error: Cannot Find Initializer

How I've previously used the class in Objective C:

[RKDropdownAlert title:@"Error: Invalid Characters!" message:@"Please remove special characters from the field." backgroundColor:[UIColor redColor] textColor:[UIColor whiteColor] time:3];

I've been looking at this for over a hour now. Can anyone lend a hand? Thanks!

Upvotes: 2

Views: 288

Answers (1)

Palle
Palle

Reputation: 12109

As this is a class method and not an initializer, you have to call it with

RKDropdownAlert.title(title:NSString, message:NSString, ...)

if you want to make the method an initializer, you have to change the method declaration in the Objective-C file to

-(instancetype)initWithTitle:(NSString*)title message:(NSString*)message backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor time:(NSInteger)seconds;

and in the implementation:

-(instancetype)initWithTitle:(NSString*)title message:(NSString*)message backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor time:(NSInteger)seconds
{
    self = [super init];
    if (self) {
        //Create your alert here
    }
    return self;
}

Upvotes: 2

Related Questions