简体   繁体   中英

Saving the parsed data into an array

I'm following this tutorial , and currently im trying to save the data in the array. Actually this tutorial is doing what im trying to do in my table . At the moment I m getting my data through this

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
}

In my class GroupListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource I've declared var friendList = [Conversations]() which is filled up from the json in the request self.friendList = Conversations.getModelArray(fromArray: data)

My question would be how to make it like in that tutorial , since he's hardcoded the array.

I've tried something like that from bellow but i receive

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)}
    }
}

Appreciate.

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

this function requires an array of dictionaries as input, bur you pass an array of Conversation objects to it in:

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

so you need to have an array of dictionaries in order to initialize the dataArray constant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM