在Swift 4中使用@objc已弃用,但该怎么做?

时间:2018-06-03 03:42:47

标签: ios swift xcode swift4

因为在Swift 4中不推荐使用@objc,所以我收到一条新的错误消息。这是其中之一:

  

tableView:numberOfRowsInSection:]已弃用,将被删除   斯威夫特4;添加明确的' @ objc'发出声明   Swift 4中的Objective-C入口点并禁止显示此消息

我有点搜索,并找到了这个答案:https://stackoverflow.com/a/44380886/9886916 - 但当我将 Swift 3 @objc Inference 更改为默认时,应用程序崩溃了。这是因为我有tableView行。它们看起来像这样:

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)
        }
    }

当我使用@objc func ...并将 Swift 3 @objc Inference 设置为 ON 时,该应用运行正常,但由于它已被弃用,我想我不应该做这个?这里有任何提示或解决方案吗?

编辑:这是完整的错误消息:

  

* /Users/Me/Documents/XCODE/Prosjekter/MyProject/MyProject/JHACategoriesController.swift:116:5:   隐式Objective-C入口点 - [MyProject.JHACategoriesController   tableView:heightForFooterInSection:]已弃用,将被删除   在Swift 4;添加明确的' @ objc'发出声明   Swift 4中的Objective-C入口点并禁止显示当前消息   堆栈跟踪: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视图] + 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图形服务
  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:   隐式Objective-C入口点 - [MyProject.JHACategoriesController   tableView:numberOfRowsInSection:]已弃用,将被删除   斯威夫特4;添加明确的' @ objc'发出声明   Swift 4中的Objective-C入口点并禁止显示当前消息   堆栈跟踪: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视图] + 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图形服务
  0x000000010f9afa35 GSEventRunModal + 62 27 UIKit
  0x000000010999b018 UIApplicationMain + 159 28 MyProject
  0x00000001062baf10 main + 55 29 libdyld.dylib
  0x000000011163c954 start + 1 libc ++ abi.dylib:终止于   NSException类型的未捕获异常

1 个答案:

答案 0 :(得分:4)

问题似乎主要是您没有更新方法签名。例如,你有

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

那是错的。它应该是

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


等等。只需通过文档或标题中的所有声明,然后修复它们以匹配更新的签名。

还要确保此代码位于UITableViewController或显式采用UITableViewDataSource和UITableViewDelegate的视图控制器中。

编辑您现在已经发布了代码(https://pastebin.com/dxSM3SJw),并且清楚地表明您没有按照我的说法进行操作。好的,我要重复一遍。请仔细阅读 我在说什么。

您的视图控制器声明是:

class JHACategoriesController: UIViewController {

这与我所说的相反。我说:

  

还要确保此代码位于UITableViewController或显式采用UITableViewDataSource和UITableViewDelegate的视图控制器中。

你没有这样做。做吧。您需要此声明:

class JHACategoriesController: UIViewController, UITableViewDelegate, UITableViewDataSource {


然后,您需要将所有委托和数据源方法修复为正确的更新签名。你还没有那样做。例如,您在pastebin中发布的代码具有以下声明:

func tableView(_ tableView: UITableView, numberOfSectionsInTableView indexPath: IndexPath) -> Int {
那是错的。您在问题中发布的代码具有不同的签名:

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

但那也是错的。正确的签名是:

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

我向您保证,当您为所有委托和数据源方法修复这些问题时,您的所有问题都将消失。