swift - 仅当 iOS11 可用时才包含类的扩展

标签 swift drag-and-drop ios11 bridging-header objc-bridging-header

我正在尝试扩展一个用 Obj-C 编写的类,并包含一个用 Swift 编写的扩展,使其符合 UIDropInteractionDelegate,如下所示:

@available(iOS 11.0, *)
extension NoteEditViewController: UIDropInteractionDelegate {
    @available(iOS 11.0, *)
    public func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
        let operation: UIDropOperation
        if session.localDragSession == nil {
            operation = .forbidden
        } else {
            // If a local drag session exists, we only want to move an
            // existing item in the pin board to a different location.
            operation = .forbidden
        }
        return UIDropProposal(operation: operation)
    }

    @objc(setupDropInteractions)
    @available(iOS 11.0, *)
    func setupDropInteractions() {
        // Add drop interaction
        self.view.addInteraction(UIDropInteraction(delegate: self))
    }
}

我的问题是 Project_Name-Swift.h 文件包含以下无法编译的代码:

@class UIDropInteraction;
@protocol UIDropSession;
@class UIDropProposal;

// This line is causing the issue saying "'UIDropInteractionDelegate' is partial: introduced in iOS 11.0"
@interface NoteEditViewController (SWIFT_EXTENSION(Bloomberg_Professional)) <UIDropInteractionDelegate>
- (UIDropProposal * _Nonnull)dropInteraction:(UIDropInteraction * _Nonnull)interaction sessionDidUpdate:(id <UIDropSession> _Nonnull)session SWIFT_WARN_UNUSED_RESULT SWIFT_AVAILABILITY(ios,introduced=11.0);
- (void)setupDropInteractions SWIFT_AVAILABILITY(ios,introduced=11.0);
@end

编译器提示该文件中的接口(interface)是部分的。

'UIDropInteractionDelegate' is partial: introduced in iOS 11.0

我假设包含 @available(iOS 11.0, *) 会生成一个 SWIFT_AVAILABILITY(ios,introduced=11.0) 来封装整个接口(interface),但我是错了。

有办法解决这个问题吗?

更新


我实现了一个玩具示例。

这是玩具 ViewController:

enter image description here

这是快速扩展:

enter image description here

这里是生成的 dnd_toy-Swift.h 文件。

enter image description here

你是对的,这只是一个警告,但我的问题是我们必须将所有警告视为我们项目中的错误。

关于如何从此处消除此警告的任何想法?

最佳答案

您所做的一切都是正确的,即使不是非常迟钝,投诉也是准确的。

基本上,由于您已经(正确地)将该函数标记为在 iOS 11 中可用,编译器将警告您在任何针对

因此,您可以通过将部署目标设置为 iOS 11 来消除投诉,这意味着 iOS 10 的用户根本无法安装它。或者,您可以使用新的(对 obj-c)@available 结构来防止 API 的使用。

if (@available(iOS 11, *)) {
    [self setupDropInteractions];
}

Xcode 8 不支持此构造,因为它是 Swift 提供的#available 构造的最新反向端口。

更新

我正在澄清我的出发点。看来我无法重现提问者所遇到的情况,所以我正在演示我能够做什么。

我可以生成 2 个不同的编译器警告,它们相似但似乎与原始问题不相同。

这是我从 my_project-Swift.h 生成的 objc 接口(interface):

@interface NoteEditViewController (SWIFT_EXTENSION(conditional_class_declaration)) <UIDropInteractionDelegate>
    - (UIDropProposal * _Nonnull)dropInteraction:(UIDropInteraction * _Nonnull)interaction sessionDidUpdate:(id <UIDropSession> _Nonnull)session SWIFT_WARN_UNUSED_RESULT SWIFT_AVAILABILITY(ios,introduced=11.0);
    - (void)setupDropInteractions SWIFT_AVAILABILITY(ios,introduced=11.0);
@end

问题 1:声明和 objc 属性以符合协议(protocol)

enter image description here

问题 2:使用扩展中声明的方法

enter image description here

有了重现编译错误的更多信息,我将能够提供更多帮助。

更新 2:希望是最后一次

我发现我们之间的行为差​​异是由于我的项目是使用 Xcode 8.3 创建的,然后迁移到 9。发生这些事情后,build设置似乎有所不同。有问题的设置是 CLANG_WARN_UNGUARDED_AVAILABILITY,我认为这是 Xcode 9 的新设置。

在迁移过程中,项目最终是这样的:

  • 这映射到 .pbxproject 文件中的术语 YES enter image description here

创建新项目后:

  • 这映射到术语 YES_AGGRESSIVE.pbxproject 文件 enter image description here

此设置在 WWDC2017 - What's new in LLVM 中讨论,但他们所说的任何内容都不会暗示这种微小的行为差异。我猜这是 clang 的错误以及它如何处理两个设置之间的差异(但我欢迎其他输入)。我相信您已经明白,这段代码在 iOS 10 上运行良好。此外,如果您将设置更改为简单的"is",您仍然会收到有关 iOS 11 API 的正确警告。

关于swift - 仅当 iOS11 可用时才包含类的扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44974445/

相关文章:

swift - 服务扩展和内容扩展的供应配置文件

java - SWT Droptarget 重叠问题

ios - 向上滚动时隐藏大标题

ios - UITableview 在 ios 11 中闪烁,而在 iOS 10.2 中正常工作

swift - Core ML iOS 11 无法正确识别图像

ios - 更新 UILabel Swift

ios - Artefacts 在 Swift Xcode 中画线

ios - UITableView 滚动时跳动和闪烁

HTML5 原生拖放回退 Javascript 库?

java - 拖放 JLabel : how to get the drag source