gpt4 book ai didi

ios - Swift 不识别这个特定的 Objective-C 类构造函数

转载 作者:行者123 更新时间:2023-11-28 07:05:21 24 4
gpt4 key购买 nike

我正在尝试使用这个类

REDActionSheet.h

我已经将它导入到我的桥接头和所有东西中。我还有其他 Objective C 类。 XCode 似乎检测到类,我可以正常初始化它。

但是,Swift 似乎不认识这个类的构造函数

这是它在 Objective C 中的初始化方式

REDActionSheet *actionSheet = [[REDActionSheet alloc] initWithCancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitlesList:@"1", @"2", @"3", nil];

但是当我尝试在 Swift 中使用它时。 Swift 不识别构造函数。对于其他库,Swift 似乎足够智能来检测构造函数。 XCode 代码完成似乎也没有检测到构造函数

只能这样初始化了。

var actionSheet: REDActionSheet = RedActionSheet();

通常 XCode 会足够聪明,给我这样的构造函数

var actionSheet: REDActionSheet = REDActionSheet(cancelButtonTitle: "Cancel", destructiveButtonTitle: "Destroy", otherButtonTitlesList: ["1", "2", "3"], nil);

但是对于这个类,好像不是这样

因为我没有 Objective C 的经验,所以我不知道是什么让 Swift 识别 Objective C 的构造函数

有什么想法吗?

最佳答案

问题是初始化器是一个可变的 Obj-C 方法,它不会自动导入到 Swift 中。

This answer描述了如何将 C 可变参数方法公开给 Swift。

如果这有点多,您可以创建一个带有固定数量参数的小型 Obj-C 扩展:

MyExtension.h

@interface REDActionSheet (MyExtension)
- (instancetype)initWithCancelButtonTitle:(NSString*)cancelButtonTitle
destructiveButtonTitle:(NSString*)destructiveButtonTitle
otherButtonTitle:(NSString*)otherButtonTitle;
@end

MyExtension.m

@implementation REDActionSheet (MyExtension)

- (instancetype)initWithCancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitle:(NSString*)otherButtonTitle
{
return [self initWithCancelButtonTitle:cancelButtonTitle
destructiveButtonTitle:destructiveButtonTitle
otherButtonTitlesList:otherButtonTitle, nil];
}

@end

关于ios - Swift 不识别这个特定的 Objective-C 类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com