Unfortunately (or fortunately depending on how you look at it), exactly what you're trying to do is not possible with Swift or Objective-C from a language perspective. The @available
attribute you're using is to tell the compiler that this class is only available if you're compiling a target with a deployment target greater than what you specified.
@MartinR made mention of the #available and the general pattern that you should employ to achieve your goal.
The reason these 2 things are available and necessary to support backward compatibility is because compiled code doesn't necessarily have all of the information about the runtime. The compiler/linker check for the existence of APIs, classes, etc by using the headers and module maps of the SDK at compile time. But, your app is linked with the system frameworks dynamically at runtime. This pattern is why your app doesn't have to be distributed with copies of every part of the SDK that it uses.
In your case, you have SDK9 and are trying to support a runtime of iOS 8 or less. The SwipeTableViewCell
doesn't work in iOS 8 likely because it uses APIs that are only available in iOS 9. All of this doesn't prevent SwipeTableViewCell
from existing on iOS 8 devices, it just prevents it from working.
You really should evaluate whether you need to support iOS 8 at all. If you change your target's deployment target to iOS 9 and release it to the App Store, people running iOS 8 won't get the update or be able to install it.
Update
After doing some thinking, and some research, I came up with a possible solution for your situation. I only tested it in an environment that relies on a storyboard for the UI, but I think it can gracefully bridge your iOS targets.
import UIKit
class ViewController: UITableViewController {
@IBOutlet var staticCell: Any? // In the storyboard, tell the cell to by a "MySwipeableCell"
override func viewDidLoad() {
super.viewDidLoad()
if staticCell is SwipeTableViewCell {
print("This class was converted magically, which means we're on iOS 9 or later")
}
}
}
class SwipeTableViewCell: UITableViewCell {
// Standin for your library class
}
class MySwipeableCell: UITableViewCell {
override func awakeAfter(using aDecoder: NSCoder) -> Any? {
if #available(iOS 9, *) {
// This will replace the MySwipeableCell with an instance of SwipeTableViewCell instead
return SwipeTableViewCell.init(coder: aDecoder)
}
return self
}
}