我目前正在学习使用 Swift 进行 iOS 开发。我正在制作一个简单的天气应用程序。我有几个按钮可以获取未来 4 小时的天气情况。我从 dark sky API 获取天气,以 JSON 格式返回。我终于弄清楚如何从 JSON 中检索正确的数据,并创建了一个用于获取数据的类。
typealias WeatherCallback = (Weather) -> Void
typealias ErrorCallback = (Error) -> Void
class NetworkManager {
class func getWeather(latitude: String, longitude: String, onSuccess: WeatherCallback? = nil, onError: ErrorCallback? = nil) {
let urlString = "https://api.darksky.net/forecast/(api key)/" + latitude + "," + longitude + "?units=si"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
onError?(error!)
} else {
do {
let json = JSON(data: data!)
let weather = Weather()
DispatchQueue.main.async {
weather.updateWithJSON(dict: json["currently"], hour: 0)
for i in 1...4 {
weather.updateWithJSON(dict: json["hourly"]["data"][i], hour: i)
}
}
onSuccess?(weather)
}
}
}.resume()
}
}
我有一个不同的类来存储检索到的数据。
class Weather {
public var currentTime: String = ""
public var currentTemperature: String = ""
public var nextHourTime: String = ""
public var nextHourTemperature: String = ""
public var nextTwoHourTime: String = ""
public var nextTwoHourTemperature: String = ""
public var nextThreeHourTime: String = ""
public var nextThreeHourTemperature: String = ""
public var nextFourHourTime: String = ""
public var nextFourHourTemperature: String = ""
func updateCurrentData(time: String, temp: String) {
currentTime = time
currentTemperature = temp
}
func updateWithJSON(dict: JSON, hour: Int){
switch hour {
case 0:
currentTime = dict["time"].stringValue
currentTemperature = dict["temperature"].stringValue
case 1:
nextHourTime = dict["time"].stringValue
nextHourTemperature = dict["temperature"].stringValue
case 2:
nextTwoHourTime = dict["time"].stringValue
nextTwoHourTemperature = dict["temperature"].stringValue
case 3:
nextThreeHourTime = dict["time"].stringValue
nextThreeHourTemperature = dict["temperature"].stringValue
case 4:
nextFourHourTime = dict["time"].stringValue
nextFourHourTemperature = dict["temperature"].stringValue
default:
print("rip")
}
}
}
我的问题是,我无法从
Weather
中访问类 ViewController
中存储的值。
例如
让天气 = Weather() 打印(天气.当前时间)
任何帮助我指明正确方向的帮助都会很棒!
您可以通过创建
Weather
类 Singleton
来做到这一点。使用单例 该对象仅存在一个副本,并且状态可由任何其他对象共享和访问。
class Weather {
public static var sharedInstance = Weather()
...
private init() {
}
}
并像这样获取单例对象
let weather = Weather.sharedInstance // it will give you the same instance of Weather every time you call it.
您可以使用委托模式在视图控制器中获取结果。首先创建一个这样的协议:
protocol YourDelegateProtocol {
func getDataFromServer(data: String)
}
之后,在 NetworkManager 类中创建此协议的实例,如下所示:
class NetworkManager {
var delegate: YourDelegateProtocol?
}
收到响应后,将响应从那里发送到您的视图控制器:
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
onError?(error!)
} else {
do {
let json = JSON(data: data!)
delegate.geDataFromServer(json as String)
}
onSuccess?(weather)
}
}
}.resume()
之后像这样获取视图控制器中的数据
class WeatherData: UIViewController, YourDelegateProtocol {
var yourModelClass = YourModelClass() //init model class globally.
func getDataFromServer(data: String) {
// populate json String into your model class. Then you are ready to go.
}
}
您可以在 ViewController 中创建请求 getWeather。此函数使您可以访问作为完成处理程序的 Weather 对象(WeatherCallback = (Weather) -> Void)。
这里可以使用单例设计模式。创建一个 Singleton
Weather
类,每当您收到 getWeather
函数的响应时,都会更新您的 Weather
单例类。当您想要用户响应时,只需从 Weather
类获取值即可。