繁体   English   中英

在 Swift 中使用 Objective-C 类时“无法找到初始化程序”

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

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

Objective-C 类初始化 (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];

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

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

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

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

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

并在实施中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM