gpt4 book ai didi

json - Swift 3 - 将模拟的 JSON 声明为来自服务器的响应

转载 作者:搜寻专家 更新时间:2023-11-01 06:33:14 25 4
gpt4 key购买 nike

我正在使用 swift 3.0 制作应用程序。但是我有一个问题,因为在 API REST 中仍然没有实现服务,我正在创建一个模拟的 JSON 来继续工作。但是,正如您将在图片中所有解释的最后看到的问题是,我不知道如何声明 JSON“-.- ....基本上程序将调用服务器并响应使用 JSON(现在我将它传递给“模拟”,您将在代码中看到它)。使用 JSON 将它与 ObjectMapper 映射到某些模型(我传递代码),以便最终应用程序有一个对象。

Error declaring Simulated JSON

当 JSON 来自服务器或在本例中为模拟 JSON 时,我必须映射这三个模型。

首先是“LegendEntriesModel”:

import Foundation
import ObjectMapper
import AlamofireDomain

class LegendEntriesModel: Mappable {


fileprivate var _id_snapshot: String?
fileprivate var _date: String?
fileprivate var _deliverables: [DeliverablesModel]?



init(){}

required init?(map: Map) { }

func mapping(map: Map) {
self.id_snapshot <- map["id_snapshot"]
self.date <- map["date"]
self.deliverables <- map["deliverables"]
}

var id_snapshot: String {
get {
if _id_snapshot == "" {
_id_snapshot = ""
}
return _id_snapshot!
}
set {
_id_snapshot = newValue
}
}


var date: String {
get {
if _date == "" {
_date = ""
}
return _date!
}
set {
_date = newValue
}
}


var deliverables: [DeliverablesModel] {
get {
if _deliverables == nil {
_deliverables = []
}
return _deliverables!
}
set {
_deliverables = newValue
}
}



//MARK: RELEASE MEMORY BETWEEN OBJECT AND API REST (BROKE DEPENDENCIS)
func copy()->LegendEntriesModel {

let legendEntriesModel = LegendEntriesModel()

legendEntriesModel.id_snapshot = self.id_snapshot
legendEntriesModel.date = self.date
legendEntriesModel.deliverables = copyDeliverables()

return legendEntriesModel
}

func copyDeliverables() -> [DeliverablesModel]{
var newArray: [DeliverablesModel] = []
for item in deliverables {
newArray.append(item.copy())
}
return newArray
}
}

第二个是“DeliverablesModel”

import Foundation
import ObjectMapper
import AlamofireDomain

class DeliverablesModel: Mappable {


fileprivate var _id: String?
fileprivate var _type: String?
fileprivate var _url_layer: String?
fileprivate var _options: OptionsDeliverablesModel?



init(){}

required init?(map: Map) { }

func mapping(map: Map) {
self.id <- map["id"]
self.type <- map["type"]
self.url_layer <- map["url_layer"]
self.options <- map["options"]
}

var id: String {
get {
if _id == "" {
_id = ""
}
return _id!
}
set {
_id = newValue
}
}


var type: String {
get {
if _type == "" {
_type = ""
}
return _type!
}
set {
_type = newValue
}
}


var url_layer: String {
get {
if _url_layer == "" {
_url_layer = ""
}
return _url_layer!
}
set {
_url_layer = newValue
}
}

var options: OptionsDeliverablesModel {
get {
if _options == nil {
_options = OptionsDeliverablesModel()
}
return _options!
}
set {
_options = newValue
}
}

//MARK: RELEASE MEMORY BETWEEN OBJECT AND API REST (BROKE DEPENDENCIS)
func copy()->DeliverablesModel {

let deliverablesModel = DeliverablesModel()

deliverablesModel.id = self.id
deliverablesModel.type = self.type
deliverablesModel.url_layer = self.url_layer
deliverablesModel.options = self.options

return deliverablesModel
}
}

最后一个是“OptionsDeliverablesModel”:

import Foundation
import ObjectMapper
import AlamofireDomain

class OptionsDeliverablesModel: Mappable {


fileprivate var _type: String?
fileprivate var _max_range: Float?
fileprivate var _min_range: Float?
fileprivate var _title: String?
fileprivate var _initial_max_value: Float?
fileprivate var _initial_min_value: Float?
fileprivate var _id: String?



init(){}

required init?(map: Map) { }

func mapping(map: Map) {
self.type <- map["type"]
self.max_range <- map["max_range"]
self.min_range <- map["min_range"]
self.title <- map["title"]
self.initial_max_value <- map["initial_max_value"]
self.initial_min_value <- map["initial_min_value"]
self.id <- map["id"]
}

var type: String {
get {
if _type == "" {
_type = ""
}
return _type!
}
set {
_type = newValue
}
}


var max_range: Float {
get {
if _max_range == 0 {
_max_range = 0
}
return _max_range!
}
set {
_max_range = newValue
}
}

var min_range: Float {
get {
if _min_range == 0 {
_min_range = 0
}
return _min_range!
}
set {
_min_range = newValue
}
}


var title: String {
get {
if _title == "" {
_title = ""
}
return _title!
}
set {
_title = newValue
}
}

var initial_max_value: Float {
get {
if _initial_max_value == 0 {
_initial_max_value = 0
}
return _initial_max_value!
}
set {
_initial_max_value = newValue
}
}

var initial_min_value: Float {
get {
if _initial_min_value == 0 {
_initial_min_value = 0
}
return _initial_min_value!
}
set {
_initial_min_value = newValue
}
}

var id: String {
get {
if _id == "" {
_id = ""
}
return _id!
}
set {
_id = newValue
}
}




//MARK: RELEASE MEMORY BETWEEN OBJECT AND API REST (BROKE DEPENDENCIS)
func copy()->OptionsDeliverablesModel {

let optionsDeliverablesModel = OptionsDeliverablesModel()

optionsDeliverablesModel.type = self.type
optionsDeliverablesModel.max_range = self.max_range
optionsDeliverablesModel.min_range = self.min_range
optionsDeliverablesModel.title = self.title
optionsDeliverablesModel.initial_max_value = self.initial_max_value
optionsDeliverablesModel.initial_min_value = self.initial_min_value
optionsDeliverablesModel.id = self.id

return optionsDeliverablesModel
}
}

有了这三个“模型”,我就可以将 JSON 映射到 DAO 类中,但问题就出在这里,因为我不知道如何传递我模拟的 JSON。

代码如下:

import AlamofireDomain
import Alamofire
import ObjectMapper

class DeliverablesLegendDAO : SimpleDAO {

var deliverables = Dictionary<String, Any>()

deliverables = [{"legendEntries": [{"id_snapshot": "123","date": "2016-10-20","deliveries": [{"id": "12","type": "RGB","url_layer":"topp:states","options": [{"type": "range","max_range": 100,"min_range": 0,"title": "Option RGB","initial_max_value": 100,"initial_min_value": 0,"id": "depth"}]}]}]}]

func snapshots(_ parameters: String,
callbackFuncionOK: @escaping (LegendEntriesModel)->(),
callbackFunctionERROR: @escaping (Int,NSError)->()) {



Alamofire.request(parameters,
method: .post,
encoding: JSONEncoding.default)
.responseJSON { response in
if response.result.isSuccess{
if let status = response.response?.statusCode {
switch(status){
case 200:
let value = response
let legendEntries = Mapper<LegendEntriesModel>().map(JSONObject: value)
callbackFuncionOK(legendEntries!)

default:
break
}
}
}

else {
var statusCode = -1
if let _response = response.response {
statusCode = _response.statusCode
}
var nsError: NSError = NSError(domain: Constants.UNKNOWN_HTTP_ERROR_MSG,
code: Constants.UNKNOWN_HTTP_ERROR_ID,
userInfo: nil)
if let _error = response.result.error {
nsError = _error as NSError
}
callbackFunctionERROR(statusCode,nsError)
}

}
}

}

正如您在图片中看到的,我声明我的模拟 JSON 是错误的,然后使用“LegendDeliveriesModel”将其映射到一个对象。我该怎么做?

Error declaring simulated JSON

如果你还需要什么,请告诉我。我重复一遍,问题出在 JSON 模拟语句中,我不知道如何将它传递给 DAO 并映射它。

最佳答案

您好,不确定您是否愿意接受,但最好尝试在文件中创建一个 JSON 并使用 Bundle 加载它,如下所示:

func loadJsonFrom(fileName: String) -> NSDictionary {

let path = Bundle.main.path(forResource: filename, ofType: "json")
let jsonData = try! Data(contentsOf: URL(fileURLWithPath: path!))
let jsonResult: NSDictionary = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! NSDictionary
return jsonResult
}

关于json - Swift 3 - 将模拟的 JSON 声明为来自服务器的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44807980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com