1

When trying to save a Dictionary<String, Any> into a Array <Dictionary<String, Any>> system throws an exception EXC_BREAKPOINT. The same code but instead of AnyI used String was working fine: Dictionary<String, String > stored in an Array <Dictionary<String, String >>

Why this happens ? I suppose it has problems with Any type, however I can't solve this issue, because I need that Dictionary to store different kind of objects

Update If I declare array var mListItems = Array<Dictionary<String, Any>>(); as a class property then it crashes, if mListItems is declared as local variable it WORKS

1
  • Show your code, please. This works fine. Commented Jul 10, 2014 at 15:32

2 Answers 2

1

Seems there are ways to fix crash:

A - Use AnyObject instead of Any as dictionary value type

class ViewController: UIViewController {

    var mListItems = Array<Any>() // Or Array<Dictionary<String, AnyObject>>()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var characters = Dictionary<String, AnyObject>()
        characters["a"] = "A"
        mListItems.append(characters)
    }

} 

B - Use NSDictionary

var animals = NSMutableDictionary()
animals.setValue("Miao", forKey: "cat")
mListItems.append(animals) // mListItems is Array of Any
Sign up to request clarification or add additional context in comments.

1 Comment

this works in playground but not in my viewcontroller
0

It was a bug in Swift. I can't reproduce it anymore.

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.