在Swift 3中访问结构外部的值

时间:2016-10-14 09:49:08

标签: swift struct swift-structs

我尝试为我的iOS应用Swift 3实施PagingMenuController。请参阅full code example here on github。在此示例中,它们在结构PagingMenuOptionslet viewController1 = ViewController1() ...)内创建viewControllers。但是,我希望能够访问在结构PagingMenuOptions中声明为外部的ViewControllers数组,而不是在此处创建。但是我收到了错误

  

结构声明不能关闭价值' self'在外部范围内定义。

知道如何实现这个吗?

private struct PagingMenuOptions: PagingMenuControllerCustomizable {
    fileprivate var componentType: ComponentType {
        return .all(menuOptions: MenuOptions(), pagingControllers: pagingControllers)
    }

    fileprivate var pagingControllers: [UIViewController] {
        let viewController1 = ViewController1()
        let viewController2 = ViewController2()
        return [viewController1, viewController2]
    }

    fileprivate struct MenuOptions: MenuViewCustomizable {
        var displayMode: MenuDisplayMode {
            return .segmentedControl
        }
        var itemsOptions: [MenuItemViewCustomizable] {
            return [MenuItem1(), MenuItem2()]
        }
    }

    fileprivate struct MenuItem1: MenuItemViewCustomizable {
        var displayMode: MenuItemDisplayMode {
            return .text(title: MenuItemText(text: "First Menu"))
        }
    }
    fileprivate struct MenuItem2: MenuItemViewCustomizable {
        var displayMode: MenuItemDisplayMode {
            return .text(title: MenuItemText(text: "Second Menu"))
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这可以通过向struct PagingMenuOptions添加init(xxx)函数并将viewControllers数组传递给该函数来完成。