小编syd*_*yds的帖子

Swift:JSONDecoder 从 API 返回 nil

目前正在通过一个从 OpenWeatherMap API 获取和解码数据的应用程序工作,目前除了让解码器返回一些东西之外,我已经让一切正常工作。目前,解码器返回 nil,但是,我从 API 调用中获取数据字节。我不完全确定可能是什么问题。我已经根据层次结构设置了 ViewModel 结构。OPW API JSON 数据似乎是字典 key:value 对集合类型的格式,键被引号括起来,是不是我的解码器因为引号没有找到必要的信息?

获取和解码 API 调用...

@IBAction func saveCityButtonPressed() {

    if let city = cityNameTextField.text {
        let weatherURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=\(city)&APPID=8bad8461edbaf3ff50aa2f2fd8ad8a71&units=imperial")!

        let weatherResource = Resource<WeatherViewModel>(url: weatherURL) { data in
            let weatherVM = try? JSONDecoder().decode(WeatherViewModel.self, from: data)
            return weatherVM
        }
        Webservice().load(resource: weatherResource) { result in
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型

struct WeatherListViewModel {
private var weatherViewModels = [WeatherViewModel]()
}

struct WeatherViewModel: Decodable {
let name: String
let main: TemperatureViewModel
}

struct …
Run Code Online (Sandbox Code Playgroud)

json decoder swift

2
推荐指数
2
解决办法
3361
查看次数

以编程方式编码应用程序后启动屏幕

我一直在以编程方式编写一个应用程序,突然决定在启动屏幕上做一些工作,自从创建项目文件以来我就没有碰过它,并且我尝试添加 UILabel 文本以及 UIImageView (两者分别与约束),但是我似乎无法让 LaunchScreen 在模拟器上显示我的设备。

有人有在非故事板项目中使用启动屏幕的经验吗?关于如何让它发挥作用有什么建议吗?

我的启动屏幕已勾选所有适当的框,例如Use as Launch ScreenIs Initial Controller

xcode storyboard swift launch-screen

-1
推荐指数
1
解决办法
593
查看次数

标签 统计

swift ×2

decoder ×1

json ×1

launch-screen ×1

storyboard ×1

xcode ×1