userPerPer
userPerPer

Reputation: 35

How to handle array in array

I would like to ask how to handle the below array as I'm confused with this problem few days.

var testArray = [Dictionary<String, Any>]()
print(testArray)

Here is the log result:

[["timestamp": 1579668693104, "phone": 12345678, "message": hihihihi], ["phone": 44445555, "timestamp": 1579668435606, "message": hello],["timestamp": 1579668474560, "phone": 232323232, "message": yoha]]

And I wish to handle the above "testArray" into this, to put all the value of timestamp, phone and message into "insideArray", is that possible? Thank you for your help

struct insideObjects{
    var realphone: String
    var timestamp: Double
    var message: String
}

var insideArray = [insideObjects]()

Upvotes: 0

Views: 323

Answers (5)

Li Mengran
Li Mengran

Reputation: 367

You could do it as follows:

testArray.map { (dict) -> insideObjects in
    insideObjects(realphone: String(dict["phone"] as! Int), timestamp: (dict["timestamp"] as! NSNumber).doubleValue, message: dict["message"] as! String)
}

Upvotes: 2

sudhanshu-shishodia
sudhanshu-shishodia

Reputation: 1108

Try this.

let json = [["timestamp": 1579668693104, "phone": 12345678, "message": "hihihihi"], ["phone": 44445555, "timestamp": 1579668435606, "message": "hello"],["timestamp": 1579668474560, "phone": 232323232, "message": "yoha"]]

Declare struct like you have mentioned

struct InsideObjects {
    var timestamp: Double?
    var phone: Double?
    var message: String?
}

var list: [InsideObjects] = []

list = json.map {
    let timestamp = $0["timestamp"] as? Double
    let phone = $0["phone"] as? Double
    let message = $0["message"] as? String
    return InsideObjects(timestamp: timestamp, phone: phone, message: message)
}

Your list will contain all parsed elements. Hope this helped.

Upvotes: 0

Govind Ravaliya
Govind Ravaliya

Reputation: 73

Use model class for handle object

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    modelArray = insideObjects.loadInsideObjectsModelArray(forArray: testArray)

    print(modelArray[2].phone)   
}
var modelArray : [insideObjects] = []

var testArray: [[String: Any]] = [["timestamp": 1579668693104, "phone": "12345678", "message": "hihihihi"], ["phone": 44445555, "timestamp": "1579668435606", "message": "hello"],["timestamp": 1579668474560, "phone": "232323232", "message": "yoha"]]


class insideObjects : NSObject {

 struct ModelKeys {
     static let timestampKey         = "timestamp"
     static let phoneKey             = "phone"
     static let messageKey           = "message"
 }

 var timestamp : String = ""
 var phone : String = ""
 var message : String = ""

 override init() {
     super.init()
 }

 init(data:[String:Any]) {

     if let value = data[ModelKeys.timestampKey] as? String {
         self.timestamp = value
     }

     if let value = data[ModelKeys.phoneKey] as? String {
         self.phone = value
     }

     if let value = data[ModelKeys.messageKey] as? String {
         self.message = value
     }

 }

 //MARK: - GET ARRAY FROM DICTIONARY
 class func loadInsideObjectsModelArray(forArray arr:[[String : Any]]) -> [insideObjects] {
     var arrayTemp:[insideObjects] = [insideObjects]()
     for obj in arr {
         arrayTemp.append(insideObjects(data: obj))
     }
     return arrayTemp
 }

}

Upvotes: 0

PGDev
PGDev

Reputation: 24341

If the testArray is like,

let testArray = [["timestamp": 1579668693104, "phone": 12345678, "message": "hihihihi"], ["phone": 44445555, "timestamp": 1579668435606, "message": "hello"],["timestamp": 1579668474560, "phone": 232323232, "message": "yoha"]]

Use compactMap(_:) to get that working.

let insideArray = testArray.compactMap({ (dict) -> insideObjects? in
    if let phone = dict["phone"] as? Int, let message = dict["message"] as? String, let timestamp = dict["timestamp"] as? Int {
        return insideObjects(realphone: String(phone), timestamp: Double(timestamp), message: message)
    }
    return nil
})

Upvotes: 1

shraddha11
shraddha11

Reputation: 773

You can do like this.

var testArray: [[String: Any]] = [["timestamp": 1579668693104, "phone": "12345678", "message": "hihihihi"], ["phone": 44445555, "timestamp": "1579668435606", "message": "hello"],["timestamp": 1579668474560, "phone": "232323232", "message": "yoha"]]


var insideArray = [insideObjects]()

    for obj in testArray {
        insideArray.append(insideObjects(realphone: obj["phone"] as! String, timestamp: obj["timestamp"] as! Double, message: obj["message"] as! String))
    }

Upvotes: 4

Related Questions