我有 2 個網路呼叫,一個用于用戶,一個用于帖子。用戶有 id 和 name 屬性,Post 有 userId 和 title 屬性。我在 2 個單獨的陣列 fetchedUsers 和 fetchedPosts 中填充了資料。
我需要在 tableViewCell 中顯示用戶名和 post(title) 所以 Post userId 需要獲取用戶 ID
如果我的解釋不清楚,請詢問,我嘗試解釋:)
感謝您的建議幫助!
struct User: Codable {
let id: Int
let name: String
}
struct Post: Codable {
let userId: Int
let title: String
}
JSON:
Post:
{
"userId": 1,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut"
}
User:
{
"id": 8,
"name": "Nicholas Runolfsdottir V",
}
uj5u.com熱心網友回復:
您必須將“發布”和“用戶”模型合二為一。(例如 'UserPost') 首先,從網路呼叫中獲取 Post 陣列并創建 'UserPost' 陣列,然后從 User 陣列中填充 PostModel 中的 userName。
您的模型將是這樣的:
struct UserPost {
var userId: Int
var userName: String?
var postTitle: String
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420524.html
標籤:
