快速-无法使我的if语句正常运行

时间:2018-07-27 14:35:37

标签: swift string if-statement url

当我按如下方式键入代码时遇到问题,但出现错误?!

如果要在textFields上键入代码= 4234,然后点击“ ADD” =“ https://pastebin.com/raw/4234”,我要用户

else =任何网址“ http://www.example.com/

@IBAction func btnPlusPressed(_ sender: UIButton)
{

    let alert = UIAlertController(title: "Provider M3U URL", message: "Add Provided URL to add you M3U Plailist", preferredStyle: .alert)
    let loginAction = UIAlertAction(title: "ADD", style: .default, handler: { (action) -> Void in

        var url = alert.textFields![0]

        if (url.text == "535" as String ){
            let strURLl : String = "https://pastebin.com/raw/\(url.text!)"

        }else{
            let strURLl : String = "\(url.text!)"
        }


        UserDefaults .standard .set(strURLl, forKey: "URL")

        let channelsVC = self.storyboard!.instantiateViewController(withIdentifier: "ChannelsViewController") as! ChannelsViewController
        channelsVC.strURL = strURLl!
        self.navigationController?.pushViewController(channelsVC, animated: true)

    })

为什么会出现这样的错误:(使用未解析的标识符'strURLl')

1 个答案:

答案 0 :(得分:1)

在这里,您正在IF语句范围内定义常量strURLl。 这意味着您只能在周围的{ }之外使用它

if (url.text == "535" as String ){
    let strURLl : String = "https://pastebin.com/raw/\(url.text!)"
} else {
    let strURLl : String = "\(url.text!)"
}
  

else语句也有问题。

解决方案

仅在IF范围之外声明常量

let strURLl: String
if (url.text == "535" as String ){
    strURLl = "https://pastebin.com/raw/\(url.text!)"
} else {
    strURLl = "\(url.text!)"
}