无法使用"@available"将存储的属性标记为可能不可用 [英] Stored properties cannot be marked potentially unavailable with '@available'

查看:47
本文介绍了无法使用"@available"将存储的属性标记为可能不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中发现了一个有关@available的有趣问题.我添加了一个变量"UISwipeActionsConfiguration";支持iOS 11及更高版本的TableView封装为支持列表单元格的左滑编辑和删除功能.我试图模仿UITableView的文字来装饰Var,但是IDE直接编译并报告错误.我只能尝试另一个set get方法来装饰.我不禁怀疑Apple的开源Swift源代码是如何隐藏的.编译.

I found an interesting question about @available in Swift. I added a var "UISwipeActionsConfiguration" that supports iOS 11 and above to the TableView I encapsulated to support the left-slide edit and delete function of the list cell. I tried to imitate the writing of UITableView to decorate the Var, but the IDE directly compiles and reports an error. I can only try another set get method to decorate. I can't help but doubt how Apple's open source Swift source code is hidden. Compiled.

以下是Apple示例代码:

@available(iOS 2.0, *)
open class UITableView : UIScrollView, NSCoding, UIDataSourceTranslating {
    @available(iOS 10.0, *)
    weak open var prefetchDataSource: UITableViewDataSourcePrefetching?

    @available(iOS 11.0, *)
    weak open var dragDelegate: UITableViewDragDelegate?

    @available(iOS 11.0, *)
    weak open var dropDelegate: UITableViewDropDelegate?
}

以下是我的示例代码:

@available(iOS 11.0, *)
public protocol HTCTableViewDelegate {
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
}
@available(iOS 11.0, *)
public typealias EditSwipeActionsCellCallback = (_ viewModel: Any, _ sectionModel: HTCTableViewSection) -> UISwipeActionsConfiguration? 

public class JSDTableView : UITableView, UITableViewDataSource, UITableViewDelegate {
    @available(iOS 11.0, *)
    public var editSwipeActionsCallback: EditSwipeActionsCellCallback? 
    @available(iOS 11.0, *)
    weak open var jsdDelegate: HTCTableViewDelegate? 
}

我的代码无法正常编译,并且IDE报告了一条错误消息:无法使用'@available'将存储的属性标记为可能不可用

My code did not compile normally, and the IDE reported an error message: Stored properties cannot be marked potentially unavailable with'@available'

最后,我只能通过以下方式实现它:

@available(iOS 11.0, *)
public typealias EditSwipeActionsCellCallback = (_ viewModel: Any, _ sectionModel: HTCTableViewSection) -> UISwipeActionsConfiguration? 

public class HTCTableView : UITableView, UITableViewDataSource, UITableViewDelegate {
    private var _editSwipeActionsCallback: Any? = nil 
    @available(iOS 11.0, *)
    var editSwipeActionsCallback: EditSwipeActionsCellCallback? {
        get {
            return _editSwipeActionsCallback as? EditSwipeActionsCellCallback
        }
        set {
            _editSwipeActionsCallback = newValue
        }
    }
}

最终代码可以正常运行,但是我真的很想知道Swift后面的Apple开源UITableView如何使用@available(iOS 11.0,*)来修改Var.

The final code can function normally, but I really want to know how Apple's open source UITableView behind Swift implements the use of @available(iOS 11.0, *) to modify Var.

推荐答案

查看 UITableView 时看到的内容不是其工作原理的实际源代码,而是显示以下内容的头文件面向用户的实现.实际上,这很有可能就是源代码的样子:

What you're seeing when you look at UITableView is not the actual source code of how it works, but a header file showing the user-facing implementation of it. It's entirely possible that this is actually what the source code looks like:

@available(iOS 2.0, *)
open class UITableView : UIScrollView, NSCoding, UIDataSourceTranslating {
    private var _prefetchDataSource: Any? = nil
    @available(iOS 10.0, *)
    weak open var prefetchDataSource: UITableViewDataSourcePrefetching? {
        get {
            return _prefetchDataSource as? UITableViewDataSourcePrefetching
        } set {
            _prefetchDataSource = newValue
        }
    }
    // ...
}

P.S.感谢您发布此信息!您的问题是对标题错误的有效解决方案.

P.S. Thank you for posting this! Your question was a helpful solution to the error that is the title.

这篇关于无法使用"@available"将存储的属性标记为可能不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆