如何在单个变量中分配不同的类型?

时间:2017-05-15 09:16:05

标签: ios swift casting type-conversion

我有三个课程firstVCsecondVCfinalVC。 现在我想在finalVC中创建一个变量。谁能够在单个变量中获取对象类firstVCsecondVC的值。

以下示例。

class finalVC: UIViewController{
    var objfinal: some-type!
    var objFirstVC: firstVC!
    Var objSecondVC: secondVC!

    override func viewDidLoad() {
        super.viewDidLoad()
        objfinal = objFirstVC
        objfinal = objSecondVC
    }
}

1 个答案:

答案 0 :(得分:2)

var loginVc:LoginViewController?
var signupVc:SignUpViewController?
var objfinal: AnyObject?


override func viewDidLoad() {
   super.viewDidLoad()

    objfinal = signupVc
    objfinal = loginVc

     objfinal = signupVc
    objfinal = loginVc

    if(objfinal is LoginViewController)
            {
                loginVc = objfinal as! LoginViewController
                print(loginVc.strName)
            }
            else if(objfinal is SignUpViewController)
            {
                 signupVc = objfinal as! SignUpViewController
    print(signupVc.strPicUrl)
            }

        }