Reputation: 355
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
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