2

Here i want to store values into an Array from ModelClass response Dictionary.

  • Model Class Dictionary Function
func toDictionary() -> [String:Any]
    {
        var dictionary = [String:Any]()
        if inwishlist != nil{
            dictionary["Inwishlist"] = inwishlist
        }
        if image != nil{
            dictionary["image"] = image
        }
        if productId != nil{
            dictionary["product_id"] = productId
        }
        if productName != nil{
            dictionary["product_name"] = productName
        }
        if regularPrice != nil{
            dictionary["regular_price"] = regularPrice
        }
        if shortDescription != nil{
            dictionary["short_description"] = shortDescription
        }
        if specialPrice != nil{
            dictionary["special_price"] = specialPrice
        }
        if stockStatus != nil{
            dictionary["stock_status"] = stockStatus
        }
        if type != nil{
            dictionary["type"] = type
        }
        if wishlistItemId != nil{
            dictionary["wishlist_item_id"] = wishlistItemId
        }
        return dictionary
    }
  • Response i get from API
 "products" : [
      {
        "special_price" : "",
        "stock_status" : true,
        "product_id" : "879",
        "type" : "simple",
        "image" : "catalog\/product\/k\/8\/k834_8.jpg",
        "product_name" : "Bourgeat Stainless Steel Saucepan Lid 240mm",
        "regular_price" : "17.4900",
        "wishlist_item_id" : "0",
        "Inwishlist" : "OUT",
        "short_description" : null
      }    

    ]

But when I try This code :-

let dataS = self.resultGetProductRoot?.response.products[0].toDictionary().keys as! [String]

i get this error:-

Cast from 'Dictionary.Keys?' to unrelated type '[String]' always fails

And when I try This code :-

  let dataS = self.resultGetProductRoot?.response.products[0].toDictionary().keys
  var dat = [String]()
  dat = dataS

i got below error:-

Cannot assign value of type 'Dictionary.Keys?' to type '[String]'

2 Answers 2

3

Since Xcode 7.0 beta 6 in order to have the keys of your Dictionary as an array you can achieve it like this:

let productKeys = self.resultGetProductRoot?.response.products[0].toDictionary().keys
var productKeysList = Array(productKeys)
Sign up to request clarification or add additional context in comments.

Comments

1

Collections are very type safe in Swift, it is not like Java/Objective-C for example which are dynamic. So, in this case you would convert the array manually using loops or using functional programming.

For example (using functional programming):

let dictionaryOrEmpty = dictionary ?? [:] // Make an empty dictionary if the dictionary itself is nil
let keysCollection = dictionaryOrEmpty.keys
let strings = keysCollection.map { $0.description } // Get the "string representation" of each of the keys

You can make it one-liner using the following:

let strings = (dictionary ?? [:]).keys.map { $0.description }

Performance gain (both memory and time) could be done using the lazy collections also (if all what you need is iterate this as a sequence)

let strings = (dictionary ?? [:]).keys.lazy.map { $0.description }

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.