-1

So since @objc is deprecated in Swift 4, I am getting a new error messages. This is one of them:

tableView:numberOfRowsInSection:] is deprecated and will be removed in Swift 4; add explicit '@objc' to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message

I have search a bit, and found this answer: https://stackoverflow.com/a/44380886/9886916 - But when I change Swift 3 @objc Inference to Default the app crashes. This is because of the tableView lines I have. They looks like this:

func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return players.count
    }

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

        cell.gameLabel.text = self.players [indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, canEditRowAtIndexPath indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: IndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.delete) {

            players.remove(at: indexPath.row)

            self.tableView.reloadSections(IndexSet(integer: 0), with: UITableViewRowAnimation.automatic)
        }
    }

The app runs fine when I am using @objc func ... and set Swift 3 @objc Inference to ON but since it is deprecated, I guess I should not do this? Any tips or solutions here?

EDIT: This is the full error message:

* /Users/Me/Documents/XCODE/Prosjekter/MyProject/MyProject/JHACategoriesController.swift:116:5: implicit Objective-C entrypoint -[MyProject.JHACategoriesController tableView:heightForFooterInSection:] is deprecated and will be removed in Swift 4; add explicit '@objc' to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message Current stack trace: 0 libswiftCore.dylib
0x000000010ead0ff0 swift::warning(unsigned int, char const*, ...) + 228 1 libswiftCore.dylib 0x000000010eb075b0 swift_objc_swift3ImplicitObjCEntrypoint + 514 2 MyProject
0x0000000106288d20 @objc JHACategoriesController.tableView(_:heightForFooterInSection:) + 84 3 UIKit 0x0000000109aebf53 -[UITableView _delegateWantsFooterForSection:] + 576 4 UIKit 0x0000000109db4414 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1236 5 UIKit
0x0000000109db92f1 -[UITableViewRowData numberOfRows] + 95 6 UIKit 0x0000000109ad2d7e -[UITableView noteNumberOfRowsChanged] + 117 7
UIKit 0x0000000109ad1d3f -[UITableView reloadData] + 1354 8 MyProject
0x0000000106285ea0 JHACategoriesController.viewDidLoad() + 4879 9
MyProject 0x0000000106287210 @objc JHACategoriesController.viewDidLoad() + 36 10 UIKit
0x0000000109b3ecd2 -[UIViewController loadViewIfRequired] + 1215 11
UIKit 0x0000000109b3f5b9 -[UIViewController view] + 27 12 UIKit 0x0000000109b708bd -[UINavigationController _startCustomTransition:] + 945 13 UIKit 0x0000000109b87318 -[UINavigationController _startDeferredTransitionIfNeeded:] + 714 14 UIKit 0x0000000109b88836 -[UINavigationController __viewWillLayoutSubviews] + 150 15 UIKit 0x0000000109de0af0 -[UILayoutContainerView layoutSubviews] + 231 16
UIKit 0x0000000109a6a21d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515 17 QuartzCore 0x000000010947a569 -[CALayer layoutSublayers] + 177 18 QuartzCore
0x000000010947e6a0 CA::Layer::layout_if_needed(CA::Transaction*) + 395 19 QuartzCore 0x0000000109405148 CA::Context::commit_transaction(CA::Transaction*) + 343 20
QuartzCore 0x0000000109432708 CA::Transaction::commit() + 568 21 UIKit
0x00000001099c3e7c _afterCACommitHandler + 272 22 CoreFoundation
0x000000010e31e5f0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 23 CoreFoundation 0x000000010e31e3b0 __CFRunLoopDoObservers + 430 24 CoreFoundation 0x000000010e302580 __CFRunLoopRun + 1537 25 CoreFoundation
0x000000010e302090 CFRunLoopRunSpecific + 635 26 GraphicsServices
0x000000010f9afa35 GSEventRunModal + 62 27 UIKit
0x000000010999b018 UIApplicationMain + 159 28 MyProject
0x00000001062baf10 main + 55 29 libdyld.dylib
0x000000011163c954 start + 1
/Users/Me/Documents/XCODE/Prosjekter/MyProject/MyProject/JHACategoriesController.swift:59:5: implicit Objective-C entrypoint -[MyProject.JHACategoriesController tableView:numberOfRowsInSection:] is deprecated and will be removed in Swift 4; add explicit '@objc' to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message Current stack trace: 0 libswiftCore.dylib
0x000000010ead0ff0 swift::warning(unsigned int, char const, ...) + 228 1 libswiftCore.dylib 0x000000010eb075b0 swift_objc_swift3ImplicitObjCEntrypoint + 514 2 MyProject
0x00000001062877b0 @objc JHACategoriesController.tableView(_:numberOfRowsInSection:) + 84 3
UIKit 0x0000000109b05813 -[UITableView _numberOfRowsInSection:] + 62 4 UIKit 0x0000000109db4414 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2631 5 UIKit
0x0000000109db92f1 -[UITableViewRowData numberOfRows] + 95 6 UIKit 0x0000000109ad2d7e -[UITableView noteNumberOfRowsChanged] + 117 7
UIKit 0x0000000109ad1d3f -[UITableView reloadData] + 1354 8 MyProject
0x0000000106285ea0 JHACategoriesController.viewDidLoad() + 4879 9
MyProject 0x0000000106287210 @objc JHACategoriesController.viewDidLoad() + 36 10 UIKit
0x0000000109b3ecd2 -[UIViewController loadViewIfRequired] + 1215 11
UIKit 0x0000000109b3f5b9 -[UIViewController view] + 27 12 UIKit 0x0000000109b708bd -[UINavigationController _startCustomTransition:] + 945 13 UIKit 0x0000000109b87318 -[UINavigationController _startDeferredTransitionIfNeeded:] + 714 14 UIKit 0x0000000109b88836 -[UINavigationController viewWillLayoutSubviews] + 150 15 UIKit 0x0000000109de0af0 -[UILayoutContainerView layoutSubviews] + 231 16
UIKit 0x0000000109a6a21d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515 17 QuartzCore 0x000000010947a569 -[CALayer layoutSublayers] + 177 18 QuartzCore
0x000000010947e6a0 CA::Layer::layout_if_needed(CA::Transaction*) + 395 19 QuartzCore 0x0000000109405148 CA::Context::commit_transaction(CA::Transaction*) + 343 20
QuartzCore 0x0000000109432708 CA::Transaction::commit() + 568 21 UIKit
0x00000001099c3e7c _afterCACommitHandler + 272 22 CoreFoundation
0x000000010e31e5f0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 23 23 CoreFoundation 0x000000010e31e3b0 __CFRunLoopDoObservers + 430 24 CoreFoundation 0x000000010e302580 __CFRunLoopRun + 1537 25 CoreFoundation
0x000000010e302090 CFRunLoopRunSpecific + 635 26 GraphicsServices
0x000000010f9afa35 GSEventRunModal + 62 27 UIKit
0x000000010999b018 UIApplicationMain + 159 28 MyProject
0x00000001062baf10 main + 55 29 libdyld.dylib
0x000000011163c954 start + 1 libc++abi.dylib: terminating with uncaught exception of type NSException

Jon Cayne
  • 21
  • 2
  • 7
    "So since @objc is deprecated in Swift 4" It isn't, so your premise is false. In fact, in Swift 4 it is needed _more_ than in the past. However, it is not needed for table view delegate methods. Show the context in which you are putting these lines; it sounds like you may have forgotten to adopt UITableViewDataSource explicitly, perhaps. – matt Jun 03 '18 at 03:47

1 Answers1

4

The problem appears to be mostly that you have not updated your method signatures. For example, you have

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {


That's wrong. It should be

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


And so on. Just walk thru all those declarations in the documentation or header and fix them to match the updated signatures.

Also make sure that this code is either in a UITableViewController or in a view controller that explicitly adopts UITableViewDataSource and UITableViewDelegate.

EDIT You have now posted your code (at https://pastebin.com/dxSM3SJw), and it shows clearly that you did not do what I said. Okay, I'm going to repeat it all. Please read carefully what I am saying.

Your view controller declaration is:

class JHACategoriesController: UIViewController {

That is the opposite of what I said to do. I said:

Also make sure that this code is either in a UITableViewController or in a view controller that explicitly adopts UITableViewDataSource and UITableViewDelegate.

You didn't do that. Do it. You need this declaration to be:

class JHACategoriesController: UIViewController, UITableViewDelegate, UITableViewDataSource {


Then you need to fix all the delegate and data source methods to be the correct updated signature. You still have not done that, either. For example, the code you posted in pastebin has this declaration:

func tableView(_ tableView: UITableView, numberOfSectionsInTableView indexPath: IndexPath) -> Int {


Thats wrong. The code you posted in your question has a different signature:

func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {

But that's wrong too. The correct signature is:

func numberOfSections(in tableView: UITableView) -> Int {

I assure you that when you fix these for all your delegate and data source methods, all your problems will go away.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • What about the `heightForFooterInSection` and `numberOfRowsInSection`? I tried changing to the one listed in the documentation here: https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614931-tableview , but the app stil crashes on those two methods. – Jon Cayne Jun 03 '18 at 13:42
  • Sorry, I can rephrase what I'm writing so you understand: What is the updated method for `heightForFooterInSection ` and `numberOfRowsInSection `? I tried changing to the one in https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614931-tableview , but it doesn't work. Error message: `tableView:heightForFooterInSection:] is deprecated and will be removed in Swift 4; add explicit '@objc' to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message ` – Jon Cayne Jun 03 '18 at 14:44
  • Well that is the message that displays when the app crashes, and `libc++abi.dylib: terminating with uncaught exception of type NSException` at the bottom – Jon Cayne Jun 03 '18 at 14:52
  • Oh sorry, updated the question with full error code, here is also the code: https://pastebin.com/6Fb9CXrd – Jon Cayne Jun 03 '18 at 15:22
  • Here you have it: https://pastebin.com/dxSM3SJw - I also have arrays for Blanding, Alle Spørsmål, Harmløs and Reise at the end of then project. – Jon Cayne Jun 03 '18 at 15:45
  • Okay, see my updated answer. You didn't do what my answer told you, so I repeated the key points. – matt Jun 03 '18 at 15:57
  • Oh, I was mixing the two similar projects, your fix made it work. Thanks! – Jon Cayne Jun 03 '18 at 16:18