"找不到初始化程序"在Swift中使用Objective-C类时

时间:2015-07-29 01:43:21

标签: ios swift

第一次创建一个swift应用程序,直到我在尝试将一个Objective-C类用于Swift ViewController时遇到问题时,它一直很顺利。

Objective-C类Init(RKDropdownAlert.h)

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

我的尝试:

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

错误:无法找到初始化程序

我之前如何使用目标C中的课程:

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

我现在已经看了一个多小时了。任何人都可以伸出援手吗?谢谢!

1 个答案:

答案 0 :(得分:2)

由于这是一个类方法而不是初始化程序,因此必须使用

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

如果你想让方法成为初始化器,你必须将Objective-C文件中的方法声明改为

-(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;
}

并在实施中:

 document.getElementById("bigdiv").textContent = "";