11

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.

Jersey
  • 391
  • 1
  • 4
  • 14

2 Answers2

9

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.

  • I agree with your explanation very much. What confuses me is that Swift does not claim to be completely open source, and the source code is also on github. In my opinion, is it just relatively open source than Objective-c, but some of the source code will still distinguish between declarations. Implementation will be hidden – Jersey Dec 16 '20 at 03:01
  • By the way, the actual code might have really been written in Objective-C. Most of iOS still is! – ReinstateMonica3167040 Aug 31 '22 at 21:58
1

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