字典为空时的可编码问题

时间:2019-02-15 11:22:29

标签: swift codable

我正试图迅速利用Codable协议 问题是我需要使从服务器获取的嵌套字典出队。假设 数据:其值和内部数据又一个字典目标 如果目标内部数据具有值,则可以正常工作 但是如果词典中的目标变成空的话,它说 错误-由于数据格式不正确,因此无法读取。

我的JSON响应 案例1-没有目标-面临的问题

{
  "code": "1",
  "message": "Data fetched successfully",
  "data": {
    "id": "2",
    "organization_id": "2",
    "first_name": "iOS",
    "last_name": "test",
    "user_name": "iOS",
    "email": "ios@gmail.com",
    "password": "4399578cc31cf62535a7dba566e4aea0",
    "security_hash": "fdf4d3bb5731de23b42771e01d9e0c3e",
    "google_id": "525256562",
    "facebook_id": "525256562",
    "access_code": "iosTest1",
    "gender": "",
    "contact": "1234567890",
    "user_profile": "",
    "profile_thumb": "",
    "dob": "",
    "weight": "",
    "height": "",
    "is_corporate": "0",
    "status": "1",
    "created_at": "2018-04-19 10:37:46",
    "updated_at": "2018-12-10 15:32:59",
    "profile_image_url": "",
    "goal": {}
  }
}

情况2:目标具有价值时

{
  "code": "1",
  "message": "Data fetched successfully",
  "data": {
    "id": "2",
    "organization_id": "2",
    "first_name": "iOS",
    "last_name": "test",
    "user_name": "ios",
    "email": "ios@gmail.com",
    "password": "4399578cc31cf62535a7dba566e4aea0",
    "security_hash": "fdf4d3bb5731de23b42771e01d9e0c3e",
    "google_id": "525256562",
    "facebook_id": "525256562",
    "access_code": "iosTest1",
    "gender": "",
    "contact": "1234567890",
    "user_profile": "",
    "profile_thumb": "",
    "dob": "",
    "weight": "",
    "height": "",
    "is_corporate": "0",
    "status": "1",
    "created_at": "2018-04-19 10:37:46",
    "updated_at": "2018-12-10 15:32:59",
    "profile_image_url": "",
    "goal": {
      "id": "4",
      "client_id": "2",
      "ambition_to_achieve": "adfadf",
      "current_assessment": "dfadfafa",
      "expected": "fasdfasdfsafsf",
      "expected_date": "2018-12-15",
      "description": "asdfadsfadf",
      "goal_status": "1",
      "created_at": "2018-12-12 18:15:36",
      "updated_at": ""
    }
  }
}

/// ---处理主数据的主类

struct LoggedUser: Codable {
    var dob, weight, height: String?
    let accesscode, contact, contactcode, email, facebookid: String?
    let firstname, gender, googleid, id, iscorporate, lastname: String?
    let organizationid, password, profilethumb, securityhash, status: String?
    let username, userprofile : String?
    let goal:Goal?

    enum CodingKeys: String, CodingKey {
        case accesscode="access_code"
        case contact, dob, email, gender, height, id, password, status, weight, goal
        case contactcode="contact_code"
        case facebookid="facebook_id"
        case firstname="first_name"
        case googleid="google_id"
        case iscorporate="is_corporate"
        case lastname="last_name"
        case organizationid="organization_id"
        case profilethumb="profile_thumb"
        case securityhash="security_hash"
        case username="user_name"
        case userprofile="user_profile"
    }
}

/// --->目标类

struct Goal: Codable {
    let id, clientID, ambitionToAchieve, currentAssessment: String?
    let expected, expectedDate, description, goalStatus: String?
    let createdAt, updatedAt: String?

    enum CodingKeys: String, CodingKey {
        case id
        case clientID = "client_id"
        case ambitionToAchieve = "ambition_to_achieve"
        case currentAssessment = "current_assessment"
        case expected
        case expectedDate = "expected_date"
        case description
        case goalStatus = "goal_status"
        case createdAt = "created_at"
        case updatedAt = "updated_at"
    }
}

---->更新 只是更正 发布实际回复

Get My Goal API Response ==> ["code": 1, "message": Data fetched successfully, "data": {
    "access_code" = iosTest4;
    contact = 1111111111;
    "contact_code" = 91;
    "created_at" = "2019-02-12 14:42:34";
    dob = "1996-05-10";
    email = "testing@email.com";
    "facebook_id" = "";
    "first_name" = iOS;
    gender = 1;
    goal =     (
    );
    "google_id" = "";
    height = "5.10";
    id = 35;
    "is_corporate" = 0;
    "last_name" = test;
    "organization_id" = 0;
    password = 915729f1e48bda300dabaac7d1ac8358;
    "profile_image_url" = "";
    "profile_thumb" = "";
    "security_hash" = 991f5cd86be6fc485633d1946dc84a7a;
    status = 1;
    "updated_at" = "2019-02-14 16:25:01";
    "user_name" = "testing@email.com";
    "user_profile" = "";
    weight = "0.10";
}]

1 个答案:

答案 0 :(得分:0)

当我得到2种不同格式时 当目标为空时,它给我一个数组 当有一些值时,它返回字典 因此,我通过手动将目标强制转换为[String:Any]

来明确创建自己的Data()
var loginDataDict = jsonDict["data"] as! [String:Any]
if let goalData = loginDataDict["goal"] as? [String:Any] {
   loginDataDict.updateValue(goalData, forKey: "goal")
} else {
   loginDataDict.updateValue([:], forKey: "goal")
} 

这可能不是一个正确的解决方案,但是如果与我在这里遇到的情况相同,可能会对某人有所帮助