skip to Main Content

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.

2

Answers


  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.

    Login or Signup to reply.
  2. After updating my xcode to 14 i get this error and I fixed it by adding platform iOs version:

    • open folder ios
    • open Podfile
    • in Podfile uncomment line number 2 and change "platform :ios, ‘14.0’"

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search