如何在类中定义“内联结构”而不需要先在swift(以及更多)中声明它?

时间:2017-10-31 08:21:57

标签: swift struct

我希望在班上有类似的东西:

std::string q = p + "World";

这是否可行,尤其是在结构中调用父类的函数?或者如果没有,这有什么解决方法吗?感谢。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

class ExampleViewController: UIViewController {

typealias MyDataSource = (customerList: [Customer]?, supplierList: [Supplier]?, inventoryList: [Inventory]?)

var dataSource: MyDataSource? {
    didSet {
        reloadDisplay()
    }
}

func reloadDisplay() {
    // do UI related things here
    print("reload")
}

}

以下两个语句都会导致重新加载:

exampleVC.dataSource = (customerList: [Customer](), supplierList: [Supplier](), inventoryList: [Inventory]())
exampleVC.dataSource?.customerList = []()

另一种选择如下:

struct DataSource {
    var customerList: [Customer]?
    var supplierList: [Supplier]?
    var inventoryList: [Inventory]?

    init(customerList: [Customer]? = nil, supplierList: [Supplier]? = nil, inventoryList: [Inventory]? = nil) {}
}

class ExampleViewController : UIViewController {

    var dataSource: DataSource? {
        didSet {
            reloadDisplay()
        }
    }

    func reloadDisplay() {
        // do UI related things here
    }

}