kockburn
kockburn

Reputation: 17616

AnyObject to [String: [String: String]] crashes without error

Description:

I'm trying to convert AnyObject from my [String: AnyObject] as [String: [String: String]] but just crashes without any error.

    //where my dict is the [String: AnyObject]
    if let data = dict["data"]{
        //We enter here
        print(data)
        //Crashes here.. because of conversion
        for (_, item) in data as! [String: [String: String]]{
            //Do stuff here
        }
    }

Here is the print of data:

{
    1 =     {
        col = "#006666";
        date = "2016-09-01";
        desc = "<null>";
        img = "1.png";
        tit = "title here";
        url = "id=1";
    };
    2 =     {
        col = "#006666";
        date = "2016-07-01";
        desc = "<null>";
        img = "2.png";
        tit = "title here";
        url = "id=2";
    };
    3 =     {
        col = "#006666";
        date = "2016-10-01";
        desc = "<null>";
        img = "3.png";
        tit = "title here";
        url = "id=3";
    };
    4 =     {
        col = "#006666";
        date = "2016-06-01";
        desc = "<null>";
        img = "4.png";
        tit = "title here";
        url = "id=4";
    };
}

Question:

Why am I not able to convert it AnyObject as [String: [String: String]] or even [Int: [String: String]] ?

Edit:

Converting it to [String: [String: AnyObject]] lets me get into the loop. Then when I try to handle desc it crashes:

    //In the loop
    if let description = item["desc"]{
       //Should enter here.. but it doesn't...
       if (description.isEqual(NSNull.self)){
          fotmItem.description = ""
       } else {
          //enters here and attempts to convert "<null>" to string and crashes
          fotmItem.description = description as! String
       }
   }

Upvotes: 0

Views: 209

Answers (2)

Natarajan
Natarajan

Reputation: 3271

Try to convert your [String: [String: String]] like below.

  1. AnyObject as [Any: [String: Any]], if you are not sure about the type of dic keys (1, 2, 3, etc).
  2. AnyObject as [String: [String: Any]] if you are sure that keys (1, 2, 3, etc) are strings.

So, [String: Any] will be useful whenever you don't know about the dictionary values data type(Int or String or Null, etc).

For Example:

if let data = dict["data"] as? [String: [String: Any]]{

     for (_, item) in data{
             //Do stuff here
     }
}

Updates to handle desc:

if let description = item["desc"] as? String, description != "<null>"{

     fotmItem.description = description

}else{

   fotmItem.description = ""
}

Note: Syntaxes are in Swift 3.0

Upvotes: 2

Swifty
Swifty

Reputation: 3760

You should compare item["desc"] to NSNull() instead of NSNull.self.

Replace this

if (description.isEqual(NSNull.self)){

with

if (description.isEqual(NSNull())){

Another approach would be trying to cast it as String

if let description = item["desc"] as? String {
    fotmItem.description = description
} else {
    fotmItem.description = ""
}

Upvotes: 1

Related Questions