swiftparsingswift2tmx

Error Parsing TMX file with Swift 2.1.1


Before converting my Xcode project to Swift 2 I had the following function that I was using to parse my TMX level files. It provided a variable attributeDict to use within the function.

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {

// my function code

}

After converting to Swift 2 (I am now on 2.1.1), I was prompted to change it to an @objc func and change some of the syntax. Now where the variable attributeDict was accessible within the function, now the variable is not created, leaving numerous errors

@objc func parser(parser: NSXMLParser, didStartElement elementName: String?, namespaceURI: String?, qualifiedName qName: String?, attributes: attributeDict<NSObject,AnyObject>) {

// my function code

}

I must have changed the syntax incorrectly but can't work out, from the documentation, what I've done wrong. Hoping it's obvious to someone else. Any assistance would be much appreciated.


Solution

  • The conversion has munged the declaration of attributeDict. Change that one bit back to how it was before:

    attributes attributeDict: [NSObject : AnyObject]
    

    Note that you might find you get an error if you are not allowed to use NSObject as a key for a dictionary (dictionary keys must conform to Hashable) and you should probably reconsider the type of your dictionary keys.