我完全是 swift 和 ios 编程的初学者 我在观看由 swift 1 和 xcode 6 beta 编写的编码类(class)视频时遇到了一个问题。 我知道 swift 的版本已经改变,语法也改变了很多。 我已经解决了一些问题,但仍然有一个我无法处理。 那是“调用中缺少参数标签”
以下是我的代码:
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
let locationManger:CLLocationManager = CLLocationManager()
@IBOutlet weak var location: UILabel!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var temperature: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
if(ios10()) {
locationManger.requestWhenInUseAuthorization()
}
locationManger.startUpdatingLocation()
}
func ios10() ->Bool {
return UIDevice.current.systemVersion == "10.2"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let location:CLLocation = locations[locations.count-1] as CLLocation
if(location.horizontalAccuracy > 0) {
print(location.coordinate.latitude)
print(location.coordinate.longitude)
self.updateWeatherInfo(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
locationManger.stopUpdatingLocation()
}
}
func updateWeatherInfo(latitude:CLLocationDegrees,longitude:CLLocationDegrees){
let manager = AFHTTPRequestOperationManager()
let url = "http://api.openweathermap.org/data/2.5/weather?APPID=c5a8f49ee6e86f1aaa2be178f25f37f2"
let params = ["lat":latitude,"lon":longitude,"cnt":0]
manager.get(url,
parameters: params,
success: { (operation:AFHTTPRequestOperation!, responseObject: Any!) in
print("JSON:" + (responseObject as AnyObject).description!)
updateUISuccess(responseObject as! NSDictionary!)
//that's where my error1 :missing argument label 'jsonResult' in call
}
)
}
func updateUISuccess(jsonResult:NSDictionary){
if let tempResult = (jsonResult["main"] as? [String:Double])?["type"] {
//if let tempResult = (jsonResult["main"] as? [String:Double])?["type"] current
//if let tempResult = jsonResult["main"]?["temp"]? as? Double pre
var temperature:Double
if ((jsonResult["sys"] as? [String:String])?["country"] == "US"){
//CONVERT TO FAHRENHEIT IF USER IN US
temperature = round(((temperature - 273.15) * 1.8) + 32)
}
else{
//CONVERT TO CELSIUS
temperature = round(temperature - 273.15)
}
self.temperature.text = "\(temperature)°"
print(temperature)
var name = jsonResult["name"] as! String
self.location.text = "\(name)"
var conditionArray = (jsonResult["weather"] as! NSArray)[0] as! NSDictionary
var condition = conditionArray["id"] as! Int
var sunrise = (jsonResult["sys"] as? [String:Double])?["sunrise"]
var sunset = (jsonResult["sys"] as? [String:Double])?["sunset"]
var nightTime = false
var now = NSDate().timeIntervalSince1970
if (now < sunrise! || now > sunset!) {
nightTime = true
}
//self.icon.image = UIImage(named:"sunny")
updateWeatherIcon(condition,nightTime: nightTime)
//that's where my error2 :missing argument label 'condition:' in call
}
else {
print("error!")
}
}
func updateWeatherIcon(condition: Int,nightTime: Bool){
//thunderstorm
if(condition < 300) {
if nightTime {
self.icon.image = UIImage(named:"tstorm1_night")
}
else {
self.icon.image = UIImage(named:"tstorm1")
}
}
//drizzle
else if (condition < 500) {
}
//rain
else if (condition < 600) {
}
//snow
else if (condition < 700) {
}
//fog
else if (condition < 771) {
if nightTime {
self.icon.image = UIImage(named: "fog_night")
}
else {
self.icon.image = UIImage(named: "fog")
}
}
//tornado
else if (condition < 800) {
self.icon.image = UIImage(named:"tstorm3")
}
//clear
else if (condition == 800) {
if nightTime {
self.icon.image = UIImage(named:"sunny_night")
}
else {
self.icon.image = UIImage(named:"sunny")
}
}
//few clouds
else if (condition < 804) {
if nightTime {
self.icon.image = UIImage(named:"cloudy2_night")
}
else {
self.icon.image = UIImage(named:"cloudy2")
}
}
//overcast
else if (condition == 804) {
self.icon.image = UIImage(named:"overcast")
}
//extreme
else if ((condition >= 900 && condition < 903) || (condition >= 904 && condition < 1000)){
self.icon.image = UIImage(named:"tstorm3")
}
//cold
else if (condition == 903) {
self.icon.image = UIImage(named:"snow5")
}
//hot
else if (condition == 904) {
self.icon.image = UIImage(named:"sunny")
}
//dont know
else {
self.icon.image = UIImage(named:"dono")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
print(error)
}
}
我想知道如何修复它 非常感谢!
最佳答案
该错误是因为您的函数要求您为传入的参数命名。尝试更改:
updateUISuccess(responseObject as! NSDictionary!)
致:
updateUISuccess(jsonResult: responseObject as! NSDictionary!)
或者,您可以像这样定义函数以不需要命名参数:
func updateUISuccess(_ jsonResult:NSDictionary){
注意下划线。
您的第二个错误有类似的原因,因此请更改:
updateWeatherIcon(condition,nightTime: nightTime)
至:
updateWeatherIcon(condition: condition, nightTime: nightTime)
关于ios - 如何修复 swift 3 中的 "missing argument label in call",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41789743/