【问题标题】:Open app from alertcontroller action - Swift Error [closed]从警报控制器操作打开应用程序 - Swift 错误 [关闭]
【发布时间】:2016-03-20 23:27:07
【问题描述】:

所以我编写了一些代码,它应该打开一个名为 MobilePay 的应用程序。问题是当我尝试制作 else 语句时,我的代码不会在 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)




    }

}

【问题讨论】:

  • 错误到底在哪里?完整的错误信息到底是什么?
  • 错误是它需要某种表达,我看不出它是什么。

标签: ios swift uialertcontroller


【解决方案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

【讨论】:

  • 缩进通常有帮助,这似乎是他的问题。
  • 我编辑了您的答案以添加缺少的),请注意ok1 中的闭包不是尾随闭包。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 2015-08-16
  • 1970-01-01
  • 2016-03-04
  • 2017-06-23
  • 2018-05-05
相关资源
最近更新 更多