首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将'Double.Type‘类型的值转换为所需的参数类型'Double’

无法将'Double.Type‘类型的值转换为所需的参数类型'Double’
EN

Stack Overflow用户
提问于 2017-07-24 02:14:22
回答 1查看 3.6K关注 0票数 2

我试图将GPS坐标从一个函数传递到另一个函数,但它不起作用。

代码语言:javascript
复制
func defaultCity(defLat: Double, defLon: Double){
    let url = URL(string: "api.openweathermap.org/data/2.5/weather?lat=\(defLat)&lon=\(defLon)&appid=626a124ef0844d2e021329c38a5dfafd")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil{
            print("Problem extracting data")
        } else {
            if let urlContent = data {
                do {
                    let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    print("Geoloc coords: \(jsonResult)")
                }catch{
                    print("Json Processing has failed or city name not recognized.")
                }
            }
        }
    }
    task.resume()
}

我正在尝试将defLondefLatcurrentLocation导入到defaultCity中。如何传递这些变量?

代码语言:javascript
复制
func currentlocation(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    if let location = locations.first{
        //print(location.coordinate)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        print("My location is \(myLocation)")
        let myLon = location.coordinate.longitude
        let myLat = location.coordinate.latitude
        defaultCity(defLat: myLat, defLon: myLon)
        print(location.altitude)
        print(location.speed)
    }
}

当我用下面这行代码调用viewDidLoad()中的函数时:

代码语言:javascript
复制
defaultCity(defLat : Double, defLon: Double)

这就是我找不到的错误所在。奇怪的是,红色箭头只出现在第一个Double.My viewDidLoad()方法下:

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()

    openCityAlert()
    getAndDisplayWeather()
    //for use when app is open and in background
    locationManager.requestAlwaysAuthorization()
    if CLLocationManager.locationServicesEnabled(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-24 02:17:22

您需要将其命名为

代码语言:javascript
复制
defaultCity(defLat : myLat, defLon: myLon)

定义未调用的函数时使用Double

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45268402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档