0

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]()
1
  • Are you using it to decode JSON? Commented Jan 22, 2020 at 5:37

5 Answers 5

4

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))
    }
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

0

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
 }

}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.