Swift编译器显示预期的声明错误

时间:2018-06-12 03:27:55

标签: ios swift

我正在构建一款可以帮助人们找到附近酒吧的应用。但是,在构建我的应用程序时,需要一个预期的声明"出现错误:

import UIKit
import MapKit

class BarVC: UIViewController {
    @IBOutlet weak var backBtn: UIButton!
    @IBOutlet weak var barMapBtn: UIButton!

    let regionRadius: Double = 1000

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "bars"
    request.region = regionRadius 

    @IBAction func backBtnWasPressed(_ sender: Any) {
        dismiss(animated: true)
    }
}

这段代码不在应有的位置吗?我需要遵守什么吗?请帮助谢谢!

1 个答案:

答案 0 :(得分:1)

在范围内调用您的代码

override func viewDidLoad() {
     super.viewDidLoad()

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "bars"
    request.region = regionRadius

}

或者你可以用作

 let request = MKLocalSearchRequest()
override func viewDidLoad() {
     super.viewDidLoad()
     request.naturalLanguageQuery = "bars"
    request.region = regionRadius

}