Make an extensions of generic class in Swift4 - ios

Lets assume we have a simple generic class:
class Foo<T> {
}
next add to this class an extension which implements UITableViewDatasoure:
extension Foo: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Code here
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Code here
}
}
This construction causes a compiler error with message:
#objc is not supported within extensions of generic classes or classes
that inherit from generic classes Non-'#objc' method
'tableView(_:numberOfRowsInSection:)' does not satisfy requirement of
'#objc' protocol 'UITableViewDataSource'
Anyone can tell me why? And how to fix that?

The error message seems very clear. This isn't supported. You can't attach an #objc method to a generic class in an extension. You need to define these methods in the class definition, not an extension. The "why" is "the compiler doesn't support it today." (It's likely hard to support because of specializations, but the real answer is "the compiler can't do it.")

Currently the Xcode doesn't support attach an #objc method to a generic class in an extension, you can fix it by define the method inside the class definition, as following:
class Foo<T>: UIViewController, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Code here ...
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Code here ...
}
}

Related

Override non-dynamic class delaration [duplicate]

This question already has answers here:
Overriding methods in Swift extensions
(6 answers)
Closed 1 year ago.
With new Xcode 8.3, I get the error:
Cannot override a non-dynamic class declaration from an extension
from the line of code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
How I can avoid this warning?
Extensions can add new functionality to a type, but they cannot override existing functionality.
You have two options:
Move your method inside the Class-scope, instead of extension.
Add dynamic type to your method (where it's defined), like example.
Example:
#objc open dynamic func onLog(_ message: String) -> Void {
print("Info: \(message)");
}
which means, you can't not override like super class delegate method in extension.
the way you can do is mv extension override method to subclass declare.
final class DEMO, UITableViewDataSource, UITableViewDelegate {
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return sectionHeader
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}

Swift 3: Understand syntax change for UITableViewDataSource method

I have some questions with Swift 3 function calling. Below is an example.
Old Swift:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell
Swift 3:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
That's fine with the above syntax. But now Xcode shows me an error and asks me to do like below:
#objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
I do not understand why I have to declare #objc(tableView:cellForRowAtIndexPath:).
This is happening only when I am trying to implement table view datasource methods under an extension. Also this is not happening for numberOfRowsInSection or viewForHeaderInSection.
Can anyone help me to understand why this is happening?
While I am not sure what triggers the #objc, I can suggest the following approach:
Store the tableView variable somewhere in the viewDidLoad:
let tv = tableView!
Then hover over the tableView variable and press the command button in conjunction with a click.
This should take you to the interface of a UITableView.
Then, hover over either UITableViewDelegate or UITableViewDataSource and press the command button in conjunction with a click.
Now you can see the new signatures.
A lot has changed...Happy upgrade!
Swift compiler forced to write Objc(funcName) before function if you are using this function from Objective c. According to app doc
Use the #objc(name) attribute to provide Objective-C names for
properties and methods when necessary. For example, you can mark a
property called enabled to have a getter named isEnabled in
Objective-C like this:
var enabled: Bool {
#objc(isEnabled) get {
// ...
}
}
To void this, use extension to write TableView Datasource and delegate
extension YourViewControllerName:UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell() as SplitAddContactCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80.0
}
}

Swift 3 ObjC Optional Protocol Method Not Called in Subclass

I have the following class hierarchy:
class ScrollableViewController: UIViewController, UITableViewDelegate { // ... }
That implements one UITableViewDelegate protocol method, e.g. tableView:willDisplayCellAt:
In my class SpecificScrollableViewController, which inherits from ScrollableViewController, new optional protocol methods don't get called any more, e.g. tableView(_:heightForRowAt:)
tl;dr you need to prefix the function declaration with its Objective-C declaration, e.g.
#objc(tableView:heightForRowAtIndexPath:)
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// return stuff
}
I was tipped off to this being a solution thanks to the Swift 3 Migration Guide which states:
If you implement an optional Objective-C protocol requirement in a subclass of a class that declares conformance, you’ll see a warning, “Instance method ‘…’ nearly matches optional requirement ‘…’ of protocol ‘…’”
• Workaround: Add an #objc(objectiveC:name:) attribute before the implementation of the optional requirement with the original Objective-C selector inside.
I'm fairly certain this is a bug: it appears that the the runtime dynamism that allows checking for selector capability does not get properly bridged in the Grand Swift Renaming when the protocol method is in the subclass. Prefixing the function declaration with the Objective-C name properly bridges the Swift to Objective-C and allows Swift 3 renamed methods to be queried with canPerformAction:withSender: from within Objective-C
This appears to be fixed in Swift 3.0.1 for normal subclasses, but is not fixed for generic subclasses:
class A: NSObject, UITableViewDelegate {}
class B: A {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
}
class C<T>: A {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
}
print(#selector(B.tableView(_:didSelectRowAt:))) // tableView:didSelectRowAtIndexPath:
print(#selector(C<Int>.tableView(_:didSelectRowAt:))) // tableView:didSelectRowAt:
See: https://bugs.swift.org/browse/SR-2817
To fix it: https://stackoverflow.com/a/39416386/1109892
#objc(tableView:heightForRowAtIndexPath:)
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// return stuff
}

Implementing Protocol In Generic class in swift

Let say I want to make Class TableViewDataSourceImpl like this :
class TableViewDataSourceImpl<T, U: UITableViewCell where U: ReusableCell>:NSObject, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
}
then xcode show me error type 'TableViewDataSource<T,U>' does not conform to protocol 'UITableViewDataSource'
is this a bug in xcode? or swift cannot make class with generic that implement protocol? because I gonna make tableView.datasource = tableViewDataSource
i know that we cant convert our swift generic into objective-c, but I not gonna use any objective-c here
I do this in xcode 7 beta 1
just update my xcode to beta 2 and it works!

Type 'ProfilesTableViewController' does not conform to protocol 'UITableViewDataSource' in Xcode 6 GM seed

Today I migrated to Xcode 6 GM seed and now I get the following error:
Type 'ProfilesTableViewController' does not conform to protocol
'UITableViewDataSource'.
I've overrided numberOfRowsInSection, cellForRowAtIndexPath and numberOfSectionsInTableView. In fact everything worked fine till today. I noticed that when I remove UITableViewDataSource everything is working fine, and no errors occured. So .. Is it necessary to use 'UITableViewDataSource' anymore, or just override the functions from it?
This code compiles fine:
class ProfileTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("foo", forIndexPath: indexPath) as UITableViewCell
return cell
}
}
So, you can definitely subclass UITableViewController and define its UITableViewDataSource protocol implementation.
Take notice that I am using the override keyword and also that I am not using auto-unwrapped arguments -- i.e., not like this:
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
which was correct in previous Xcode 6 betas.
My guess is that you're not implementing all the required methods, based on the error, at least.
EDIT: Because of the Xcode 6 beta syntax:
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
We had to check the unwrapped tableView. So I used:
if let tblView = tableView {
//code
}
and
if let theIndexPath = indexPath {
//code
}
So ... in order not to change my entire project, I overrided the functions like this:
override func tableView(tableView: UITableView?, didSelectRowAtIndexPath indexPath: NSIndexPath?){
and everything worked fine, except that with my changes the ProfileTableViewController doesn't conform UITableViewDataSource, and I've had to remove the data source from the class definition.

Resources