Stored properties cannot be marked potentially unavailable with '@available'

我是研究僧i 提交于 2021-01-28 02:37:19

问题


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.

Below is Apple sample code:

@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?
}

Below is my sample code:

@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? 
}

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

In the end, I can only achieve it in the following way:

@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
        }
    }
}

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.


回答1:


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. Thank you for posting this! Your question was a helpful solution to the error that is the title.



来源:https://stackoverflow.com/questions/64797366/stored-properties-cannot-be-marked-potentially-unavailable-with-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!