加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 使用继承自Storyboard中的泛型类的类时,“接口构建器文件

发布时间:2020-12-14 17:57:59 所属栏目:百科 来源:网络整理
导读:我最近从UITableViewController的简单继承重构了我的类BookTableViewController,因此它现在继承自泛型类FetchedResultsTableViewController TResultType,TCellType它本身继承自UITableViewController. 类声明如下所示: class BookTableViewController: Fetc
我最近从UITableViewController的简单继承重构了我的类BookTableViewController,因此它现在继承自泛型类FetchedResultsTableViewController< TResultType,TCellType>它本身继承自UITableViewController.

类声明如下所示:

class BookTableViewController: FetchedResultsTableViewController<Book,BookTableViewCell> {

    override func viewDidLoad() {
        // breakpoints in here do not catch!
    }

}

class FetchedResultsTableViewController<TResultType,TCellType: UITableViewCell>: 
    UITableViewController,NSFetchedResultsControllerDelegate {

    // implementation here

}

在Storyboard中,Custom类和Module都已设置,我可以单击箭头跳转到BookTableViewController类的代码,建议故事板正确链接到类.但是,当我尝试运行该应用程序时,无法识别该类 – viewDidLoad()中的代码未运行,并且在运行我的应用程序时收到以下记录的消息:

Unknown class _TtC12Reading_List23BookTableViewController in Interface Builder file.

我正在运行XCode 7.3(Swift 2.2).这是故事板的限制,一个错误,还是我错过了什么?

谢谢!

更新:

经过一些实验,它似乎与通用继承有关,而不是类的可访问性.定义了以下类:

import Foundation
import UIKit

// Both of these classes are accessible by the Storyboard
class FirstInheritance : UITableViewController{}
class SecondInheritance : FirstInheritance{}

// The generic class is also accessible
class GenericViewController<T> : UITableViewController{}

// But this class is not accessible...
class ConcreteViewController : GenericViewController<String>{}

除了ConcreteViewController之外的所有类都在Storyboard的类自动完成中建议(尽管泛型类也不起作用,因为无法在Storyboard中指定类型参数).

解决方法

游戏的后期不多,但是这个信息可能会让任何碰到线程的人都派上用场.

实际上,据我所知,现在,就Swift 3而言,在故事板中对泛型有一些尴尬的支持.

我设法编写了一个扩展UIViewController的通用基类,然后约束该泛型类的几个子类并在故事板中使用它们.

实际代码的布局与下面指定的类似:

class GenericParent: UIViewController {
    // Has a few,non-generic members ...
}

class ActualGenericClass<T>: GenericParent {
    // There are more generic members in the actual class
    var adapter: Adapter<T> = Adapter()
}

// This class is usable in Interface Builder;
// although auto-complete won't show it,fully writing down
// the class name works
class SpecificInstanceOfActualGenericClass: ActualGenericClass<Int> {
    @IBOutlet weak var tableView: UITableView!
}

令我惊讶的是,这完美无缺!

另一方面,下一个例子似乎不起作用:

class GenericCell<T>: UITableViewCell {
    var node: T? = nil
}
class SpecificCell: GenericCell<Int> {
    @IBOutlet weak var coolTitleLabel: UILabel!
}

和以前一样,在Interface Builder上显式写入单元格的类名(在单元格的动态原型上)会在表格视图单元格上设置类,并且可以看到出口.

但是,在运行时,在实例化包含单元格动态原型的UIViewController时,会显示“界面构建器文件中的未知类”警告,并且在出列单元格时应用程序崩溃.

所以@Andew Bennet,声明:

Storyboards do not support classes which inherit from generic classes

似乎不再100%真实,尽管你至少是正确的;这是我第一次成功地将这一部分拉下来,尽管只是部分!

让我觉得有些东西有效,有些则没有,但它总比没有好.

这在Java中非常直截了当……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读