从alertcontroller动作打开应用程序 - Swift错误

时间:2016-03-20 23:27:08

标签: ios swift uialertcontroller

所以我写了一些代码,应该打开一个名为MobilePay的应用程序。问题是,当我尝试制作其他内容时,我的代码将不会被SWIFT接受。谁能告诉我有什么问题? - SWIFT告诉我有错误。
错误是它需要一些表达式

@IBAction func Invoker(sender: AnyObject) {

    let alertController = UIAlertController(title: "Betal", message: "Vælg en af nedstående betalingsmulighed", preferredStyle: .ActionSheet)

    let ok1 = UIAlertAction(title: "MobilePay", style: .Default, handler: { (action) -> Void in

    let url = NSURL(string: "mobilepay://")


    if UIApplication.sharedApplication().canOpenURL(url!) == true

    {
        UIApplication.sharedApplication().openURL(url!)

    } else {


    }


    let  cancel = UIAlertAction(title: "Annuller", style: .Destructive) { (action) -> Void in


    }

    alertController.addAction(ok1)
    alertController.addAction(cancel)



    presentViewController(alertController, animated: true, completion: nil)




    }

}

1 个答案:

答案 0 :(得分:1)

看起来你的大括号搞砸了:

@IBAction func Invoker(sender: AnyObject) 
{
    let alertController = UIAlertController(title: "Betal", message: "Vælg en af nedstående betalingsmulighed", preferredStyle: .ActionSheet)

    let ok1 = UIAlertAction(title: "MobilePay", style: .Default, handler: { (action) -> Void in

        let url = NSURL(string: "mobilepay://")!

        if UIApplication.sharedApplication().canOpenURL(url)
        {
            UIApplication.sharedApplication().openURL(url)
        } 
        else 
        {
            // Unable to open url
        }
    }) // You were missing this brace

    let  cancel = UIAlertAction(title: "Annuller", style: .Destructive) { (action) -> Void in

        // Respond to the cancel action
    }

    alertController.addAction(ok1)
    alertController.addAction(cancel)

    presentViewController(alertController, animated: true, completion: nil)
}

// You had an extra brace here