调用一个在另一个函数

时间:2017-10-09 16:49:07

标签: ios swift switch-statement type-alias

目前我们有一个带有函数的类,它将typeAlias作为值返回:

class NotificationDetailFactory {

   typealias T = UIViewController & NotificationDetailType

    func getNotificationType(notificationType:PushNotificationDetail?) -> T? {

        switch notificationType?.type! {
        case .notice:
            let notificationVC = NoticeViewController()
            notificationVC.notificationType = notificationType
            return notificationVC
        case .promotion:
            let promotionVC = PromoViewController()
            promotionVC.notificationType = notificationType
            return promotionVC
}
}

switch语句中的返回值是需要访问的值(即notificationVC,promotionVC)。在视图控制器中,正在调用“getNotifcationType”函数:

 let factory = NotificationDetailFactory()

 func goToDetailView(notificationType: PushNotificationDetail) {

        switch factory.getNotificationType(notificationType: notificationType){

        case  notificationVC:
            self.presentViewController("BGMDetailNotifications", nextModule: "notice", showInNavigationController: true, showContainer: false, data: [:], animation: nil)
        case paymentVC:
            self.presentViewController("BGMDetailNotifications", nextModule: "payment", showInNavigationController: true, showContainer: false, data: [:], animation: nil)
} }

正在发生的问题是当我们尝试编译项目时,在第二部分代码中的每个case语句旁边会弹出一个错误:

  

使用未解析的标识符'notificationVC'

其中VC是在getNotificationType函数中尝试访问VC的任何内容。我猜这是因为它为第一个函数返回了一个typAlias。什么是从第一个功能访问这些VC的最佳方式?

1 个答案:

答案 0 :(得分:2)

你必须检查他们的类型。

let vc = factory.getNotificationType(notificationType: notificationType)
switch vc {
    case is NoticeViewController:
        self.present(vc, nextModule: "notice", ...)
        // here goes your code for NoticeViewController case
    case is PromoViewController:
        self.present(vc, nextModule: "payment", ...)
        // here goes your code for PromoViewController case
}