1

I am new in IOS, I get this response from backend

Optional([["contact_name": phone, "verified": <null>, "id": 22033, "entity_id": 1111, "verification_data": <null>, "entity": Recruiter, "view_scope": <null>, "contact_value": 0987654321]])

I need "contact_value" string from object.How can I get?

9
  • 2
    It's not an array, it's a dictionary. Commented Apr 4, 2018 at 9:19
  • 2
    @JoakimDanielson no, it's an array of dictionaries, note double square brackets Commented Apr 4, 2018 at 9:21
  • 1
    I think what OP should do regardless is to read about collections in The Swift Programming Language. This is an excellent source if you're new to Swift (and also more experienced) to learn the basics of the language. It's also available as an iBook. Much recommended Commented Apr 4, 2018 at 9:29
  • 2
    @RealmOfFire Did you read some basics in Swift language? see here developer.apple.com/documentation/swift/dictionary Commented Apr 4, 2018 at 9:44
  • 1
    @Mani sorry i talk about objective cand thnx for correcting me Commented Apr 4, 2018 at 9:46

5 Answers 5

3
let array: [[String: Any]] = [["contact_name": "John", "verified": true, "id": 22033, "entity_id": 1111, "verification_data": "", "entity": "Recruiter", "view_scope": "", "contact_value": 0987654321]]

for item in array {
    let contactName = item["contact_name"] as? String
    print(contactName ?? 0)
}

This is just a basic for/loop to extract the data, but there are a lot of better ways to accomplish this stuff depending on how your code is implemented.

Sign up to request clarification or add additional context in comments.

3 Comments

@mag_zbc I was just trying to show a simple for/loop and grabing the data. I updated the array to complile
Could you fix your answer so it reads the right key. Right now you're typecasting a String value to an Int.
Thank You so much
3

Instead of normal for-loop, try map concept. Note: This is tested code.

let arrayOfDict: [[String: Any]] = [["contact_name": "phone", "verified": "", "id": 22033, "entity_id": 1111, "verification_data": "", "entity": "Recruiter", "view_scope": "", "contact_value": 0987654321]]

let arraOfCtcName : [Int] = arrayOfDict.map({ (element) in
            let value = element["contact_value"]
            return (value != nil && value is Int) ? value as! Int : 0
        })

Comments

1

You could as well go a little more functional, if all you want is contact_value,

let contactValues = a.flatMap { $0 }?
    .map { $0["contact_value"] ?? 0 } as? [Int]

Comments

0
let arrayOfDict: [[[String: Any]] = ["contact_name": "phone", "verified": <null value>, "id": 22033, "entity_id": 1111, "verification_data": <null value>, "entity": "Recruiter", "view_scope": <null value>, "contact_value": 0987654321], ....]

var arrayContact = [Any]()

for dict in arrayOfDict {

  if let contactValue = dict["contact_value"] as? String { //as String
    arrayContact.append(contactValue)
  } else if let contactValue = dict["contact_value"] as? Int { //as Int
    arrayContact.append(contactValue)
  }
}

print(arrayContact)

Comments

0
let array: [[String: Any]] = [["contact_name": "John", "verified": true, "id": 22033, "entity_id": 1111, "verification_data": "", "entity": "Recruiter", "view_scope": "", "contact_value": 0987654321]]

print(array)

for item in array {
    if item["contact_value"] != nil {
        let contact_value = String(describing: item["contact_value"])
        print(contact_value)
        break
    }
}

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.