Я пытаюсь использовать протокол Codable со стремительным быстродействием. Проблема в том, что мне нужно отключить получение вложенного словаря с сервера.Предположим, что Data: его значения и внутренние данные - еще один словарь. Цель, если у цели внутри данных есть значение, она работает нормально, но если цель внутри словаря оказывается пустой, она говорит об ошибке - данные не могут быть прочитаны, потому что их нет вправильный формат.
Мой ответ 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";
}]