swift - 无法将类型 'Double.Type' 的值转换为预期的参数类型 'Double'

标签 swift api openweathermap

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

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。我如何传递这些变量?

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() 中的函数时:

defaultCity(defLat : Double, defLon: Double)

这就是我遇到无法弄清楚的错误的地方。奇怪的是红色箭头只在第一个 Double 下。我的 viewDidLoad() 方法:

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()
    }
}

最佳答案

你需要这样调用它

defaultCity(defLat : myLat, defLon: myLon)

Double 用于定义不调用的函数。

关于swift - 无法将类型 'Double.Type' 的值转换为预期的参数类型 'Double',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268402/

相关文章:

objective-c - 转换或比较“Unmanaged<NSString> 和 NSString

Swift 4 将字符串更改为 URL

Javascript - 保护 API key

API设计: HTTP Basic Authentication vs API Token

swift - 每秒更新一次实时事件 - Swift

ios - UICollectionView 中的 viewForSupplementaryElementOfKind 返回 nil 并使用 swift 使应用程序崩溃

node.js - 我应该如何做一个 elasticSearch ,从 Node.js 正确搜索查询?

javascript - Openweathermap API 调用响应错误的 json

json - 在 Swift 中从 OpenWeatherMap 解析 JSON

angularjs - 如何在angularjs中使用openweathermap api获取多个城市的数据?