gpt4 book ai didi

ios - 将解析后的数据保存到数组中

转载 作者:行者123 更新时间:2023-11-30 11:48:20 29 4
gpt4 key购买 nike

我正在关注this tutorial,目前我正在尝试将数据保存在数组中。实际上,本教程正在做我在表中尝试做的事情。目前我正在通过此获取数据

class Conversations {
var id: Int?
var name: String?
var is_group: Int?
var photo: String?

init(fromDictionary dict: Dictionary<String,Any>) {
if let tmp = dict["id"] as? Int {
self.id = tmp
}

if let tmp = dict["name"] as? String {
self.name = tmp
}

if let tmp = dict["is_group"] as? Int {
self.is_group = tmp
}

if let tmp = dict["photo"] as? String {
self.photo = tmp
}
}

class func getModelArray(fromArray array: [[String:Any]]) -> [Conversations] {
var conversations = [Conversations]()
for row in array {
conversations.append(Conversations(fromDictionary: row))
}

return conversations
}

在我的class GroupListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource中,我声明了varfriendList = [Conversations](),它是从请求中的json填充的self.friendList = Conversations.getModelArray(fromArray: data)

我的问题是如何使其像该教程中那样,因为他对数组进行了硬编码。

我已经尝试过类似的方法,但我收到

Cannot convert value of type '[Conversations].Type' to expected argument type '[[String : Any]]' at dataArray. And to be honest I don t believe also that it s good

class Conversations {
var id: Int?
var name: String?
var is_group: Int?
var photo: String?

init(fromDictionary dict: Dictionary<String,Any>) {
if let tmp = dict["id"] as? Int {
self.id = tmp
}

if let tmp = dict["name"] as? String {
self.name = tmp
}

if let tmp = dict["is_group"] as? Int {
self.is_group = tmp
}

if let tmp = dict["photo"] as? String {
self.photo = tmp
}
}

class func getModelArray(fromArray array: [[String:Any]]) -> [Conversations] {
var conversations = [Conversations]()
for row in array {
conversations.append(Conversations(fromDictionary: row))
}

return conversations
}
}


class ViewModelConversations {
private var conversations: Conversations

var isSelected = false
var id: Int? {
return conversations.id
}

var name: String? {
return conversations.name
}

var photo: String? {
return conversations.photo
}

var is_group: Int? {
return conversations.is_group
}

init(conversations: Conversations) {
self.conversations = conversations
}

}

let dataArray = [Conversations.getModelArray(fromArray: [Conversations])]

class ViewModel {
var conversations = [ViewModelConversations]()
init() {
conversations = dataArray.map { ViewModelConversations(conversations: $0)}
}
}

欣赏。

最佳答案

class func getModelArray(fromArray array: [[String:Any]]) -> [Conversations]

此函数需要一个字典数组作为输入,但您可以将一个 Conversation 对象数组传递给它:

let dataArray = [Conversations.getModelArray(fromArray: [Conversations])]

所以你需要有一个字典数组才能初始化dataArray常量

关于ios - 将解析后的数据保存到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48579786/

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