gpt4 book ai didi

ios - 混合 objective-c 项目中的 Swift "namespaces"不工作

转载 作者:行者123 更新时间:2023-11-28 12:16:17 28 4
gpt4 key购买 nike

我有一个用 objective-c 编写的自定义框架 MyFramework,我在 UIColor 的 objective-c 类别中定义了一个方法:

+ (instancetype)my_colorWithRed:(uint8_t)red 
green:(uint8_t)green
blue:(uint8_t)blue
alpha:(uint8_t)alpha NS_SWIFT_NAME(init(red:green:blue:alpha:));

我使用 NS_SWIFT_NAME 给它一个合适的 Swift 名称。但是,Swift 名称的声明与 UIKit 中的 UIColor 上已经存在的方法相同,即:

init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

现在,如果我在我的主应用程序中添加一个 Swift 文件——一个链接到 MyFramework 的混合 objective-c 和 swift 项目——并编写以下内容:

UIColor(red: 1, blue: 0, green: 0, alpha: 1)

UIKit 版本的方法被调用。但是如果我写:

MyFramework.UIColor(red: 1, blue: 0, green: 0, alpha: 1)

UIKit 版本的方法仍然被调用。

所以我想知道为什么:

  • 在不使用模块名称为方法添加前缀的情况下构建应用程序不会给我任何警告或错误,即使该方法不明确也是如此。我的印象是,如果发生这种情况,我会收到警告。
  • 指定 MyFramework 来确定方法的范围无效。

在 Swift 文件中,我不需要导入 UIKitMyFramework,因为它们已经通过 Swift 桥接 header 导入;不是直接,而是通过一些头文件间接导入它们。这可能是导致问题的原因吗?还是因为它是一个 objective-c 和 Swift 的混合项目?还是因为这是一个类别上的方法?

更新

Tarun Tyagi 的回答说明了参数类型。指定类型使其工作:

UIColor(red: UInt8(149), green: UInt8(45), blue: UInt8(152))

这会调用 MyFramework 中的方法。这意味着像我最初那样在调用中使用整数:

UIColor(red: 1, blue: 0, green: 0, alpha: 1)

匹配 UIKitinit(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) 而不是 MyFramework' s convenience init(red: UInt8, green: UInt8, blue: UInt8).

如果我创建一个采用 Int 的新类别方法

+ (instancetype)my_colorWithIntRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(NSInteger)alpha NS_SWIFT_NAME(init(red:green:blue:alpha:));

然后从主应用程序调用 UIColor(red: 1, blue: 0, green: 0, alpha: 1),该调用与该方法匹配。

添加 UIKitMyFramework 来限定调用没有区别。匹配仅基于参数类型,而不管它定义在哪个模块中。即调用:

UIKit.UIColor(red: 1, blue: 0, green: 0, alpha: 1)

没有报错,MyFramework 中的方法仍然被调用。

如果我在 UIColorMyFramework 类别中创建一个与 UIKit.UIColor 完全匹配的初始化程序:

+ (instancetype)my_colorWithFloatRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha NS_SWIFT_NAME(init(red:green:blue:alpha:));

那么根本就没有生成 Swift 版本。

如果我改为在 UIColor 的快速扩展中创建相同的方法

@objc public convenience init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
...
}

然后我得到一个预期的错误:

Initializer 'init(red:green:blue:alpha:)' with Objective-C selector 'initWithRed:green:blue:alpha:' conflicts with previous declaration with the same Objective-C selector

但是当我删除 @obj 以仅使该方法在 Swift 中可用时,我没有收到任何错误,这表明 Swift 允许在不同模块定义的扩展中使用冲突的方法名称。

所以我从我的主应用程序中进行调用:

UIColor(red: CGFloat(0.5), green: CGFloat(0.5), blue: CGFloat(1.0), alpha: CGFloat(1.0))

但我仍然没有收到关于模棱两可的方法的错误,并且调用了 UIKit 版本。此外,符合条件:

MyFramework.UIColor(red: CGFloat(0.5), green: CGFloat(0.5), blue: CGFloat(1.0), alpha: CGFloat(1.0))

同样没有区别。

所以我仍然不明白 Swift 中的隐式命名空间应该如何与扩展一起工作。好像不支持。我通读了关于 extensions 的 Swift 编程指南。并且没有提及这一点。

最佳答案

在不使用模块名称为方法添加前缀的情况下构建应用程序不会给我任何警告或错误,即使该方法不明确也是如此。我的印象是如果发生这种情况我会收到警告

This seems like -

A. Either it should be translated in Swift to use UInt8 instead of CGFloat so it is different based on type of input.

B. Or it shouldn't allow to use init(red:green:blue:alpha:) inside NS_SWIFT_NAME since it collides with already existing initializer.

Neither A nor B happens, so it seems to be a bug. You should file a radar to investigate this further.

指定 MyFramework 来确定方法的范围无效

UIColor isn't owned by your custom framework and you are only extending it. It's your responsibility to name the methods uniquely. A possible name would be my_color(red:green:blue:alpha:) inside NS_SWIFT_NAME to make it unique.

Clearly your naming init(red:green:blue:alpha:) collides with UIKit standard initializer and in case you don't get a compiler warning and are able to run, UIKit variant wins over your custom one.

希望对您有所帮助。

编辑 1:引用 https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

将其添加为此处的屏幕截图,因为链接可能会在未来的修订版中中断。

虽然引用是针对 Objective-C 而不是 Swift 扩展,但这对 UIColor 扩展有效,因为截至目前所有 UIKit 仍然是 Objective-C。

关于ios - 混合 objective-c 项目中的 Swift "namespaces"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46468011/

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