2016-09-20 63 views
2

更新到夫特3后,收到错误在以下代码:夫特3个UITableViewDataSource选择

extension MyViewController: UITableViewDataSource { 
    //... 

    func tableView(_ tableView: UITableView, 
        heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return someValue 
    } 
} 

Objective-C的方法 '的tableView:heightForRowAt:' 由方法 “的tableView(_提供:heightForRowAt :) '不要求的 选择器匹配(' 的tableView:heightForRowAtIndexPath:')

可以固定

@objc(tableView:heightForRowAtIndexPath:) 
func tableView(_ tableView: UITableView, 
       heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return someValue 
} 

任何人都可以解释Swift新版本中签名更改的动机吗?关于它的migration guide没有任何信息。

+1

看到这个http://stackoverflow.com/questions/39416385/swift-3-objc-optional-protocol-method-not-called-in-subclass –

回答

2

随着Swift 3.0的发布,为了便于阅读,库中许多方法的签名已被更改(请参阅API Design Guidelines)。

比较,例如,你引述其表示在的Xcode的代码完成建议列表的方法的电流特征:

// implementation: 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {...} 

// code completion: 
tableView(tableView: UITableView, heightForRowAt: IndexPath) 

相反用于显示冗余信息以前的实现:

// implementation: 
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {...} 

// code completion: 
tableView(tableView: UITableView, heightForRowAtIndexPath: NSIndexPath) 
               --------- ----------- 

此外,函数或方法的实现现在需要为第一个参数设置下划线(_),以便允许省略在调用函数/方法时调用参数标签(请参阅:https://stackoverflow.com/a/38192263/6793476)。

很明显,库中的某些选择器尚未更新,因此您需要提供适当的(“旧”)选择器名称(请参阅:https://stackoverflow.com/a/39416386/6793476以及有关选择器的更多信息:https://stackoverflow.com/a/24007718/6793476)。

我希望有帮助。