Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift3-单例共享UItextfield值未第二次更新_Ios_Iphone_Swift3 - Fatal编程技术网

Ios Swift3-单例共享UItextfield值未第二次更新

Ios Swift3-单例共享UItextfield值未第二次更新,ios,iphone,swift3,Ios,Iphone,Swift3,我使用Singleton传递UItextfield值这是我的Singleton类: class CityName { static var sharedInstance = CityName() private init() {} var cityName:String! } 这是我的常量类,我在这里使用这个值 let WEATHER_URL = BASE_URL + CityName.sharedInstance.cityName + API_Key 在我的视图控制

我使用Singleton传递UItextfield值这是我的Singleton类:

class CityName {
    static var sharedInstance = CityName()
    private init() {}
    var cityName:String!
}
这是我的常量类,我在这里使用这个值

let WEATHER_URL = BASE_URL + CityName.sharedInstance.cityName + API_Key
在我的视图控制器中,我使用按钮传递值:

@IBAction func searchButtonTapped(_ sender: Any) {
    CityName.sharedInstance.cityName = cityNameTextField.text!
    print(CityName.sharedInstance.cityName)
    networkConnection.getWeatherDetails {
        self.currentTempLabel.text = String(self.networkConnection.currentTemp)
        self.minTempLabel.text = String(self.networkConnection.minTemp)
        self.maxtTempLabel.text = String(self.networkConnection.maxTemp)
    }
}
在我的网络课上,我正在进行URL连接

func getWeatherDetails(completed: @escaping DownloadComplete)  {
    print(WEATHER_URL)
    Alamofire.request(WEATHER_URL).responseJSON { response in
        let result = response.result
        if let dict = result.value as? Dictionary<String, AnyObject> {
            if let main = dict["main"] as? Dictionary<String, AnyObject> {                
                if let currentTemperature = main["temp"] as? Double {
                    let kelvinToFarenheitPreDivision = (currentTemperature * (9/5) - 459.67)
                    let kelvinToFarenheit = Double(round(10 * kelvinToFarenheitPreDivision/10))
                    self._currentTemp = kelvinToFarenheit
                    print(self._currentTemp)
                }
            }
        }
        completed()
    }
}
func getWeatherDetails(已完成:@转义下载完成){
打印(天气_URL)
请求(WEATHER_URL).responseJSON{response in
让result=response.result
如果让dict=result.value作为字典{
如果让main=dict[“main”]作为字典{
如果让currentTemperature=main[“temp”]为双精度{
设kelvinToFarenheitPreDivision=(当前温度*(9/5)-459.67)
设kelvinToFarenheit=Double(圆形(10*kelvinToFarenheitPreDivision/10))
自身.\u currentTemp=kelvinToFarenheit
打印(自\u当前温度)
}
}
}
已完成()
}
}

当我第一次在文本字段中输入值时,此代码工作正常。但是,当我在文本字段中添加新值时,我的URL不会更新。

您不会在任何地方更新URL。通过
var WEATHER\u URL=BASE\u URL+CityName.sharedInstance.CityName+API\u Key将其更改为可变,然后在
searchButtonTapped()函数中更新

@IBAction func searchButtonTapped(_ sender: Any) {
    CityName.sharedInstance.cityName = cityNameTextField.text!
    print(CityName.sharedInstance.cityName)
    WEATHER_URL = BASE_URL + CityName.sharedInstance.cityName + API_Key
    networkConnection.getWeatherDetails {
        self.currentTempLabel.text = String(self.networkConnection.currentTemp)
        self.minTempLabel.text = String(self.networkConnection.minTemp)
        self.maxtTempLabel.text = String(self.networkConnection.maxTemp)
    }
}

此外,您应该为每个变量(weatherUrl、baseUrl、apiKey)使用Swift命名约定。

您没有在任何地方更新URL。通过
var WEATHER\u URL=BASE\u URL+CityName.sharedInstance.CityName+API\u Key将其更改为可变,然后在
searchButtonTapped()函数中更新

@IBAction func searchButtonTapped(_ sender: Any) {
    CityName.sharedInstance.cityName = cityNameTextField.text!
    print(CityName.sharedInstance.cityName)
    WEATHER_URL = BASE_URL + CityName.sharedInstance.cityName + API_Key
    networkConnection.getWeatherDetails {
        self.currentTempLabel.text = String(self.networkConnection.currentTemp)
        self.minTempLabel.text = String(self.networkConnection.minTemp)
        self.maxtTempLabel.text = String(self.networkConnection.maxTemp)
    }
}
此外,您应该为每个变量(weatherUrl、baseUrl、apiKey)使用Swift命名约定