Search code examples
swiftcastingchaining

Swift Chaining Commands


I'm attempting to chain some commands with casts in swift. Anyone know why the below happens? The variable assignment portion works fine, but if I try to chain them together, it produces compile errors that seem unfixable.

var tempArr = JSONDict["years"]! as [AnyObject]
var tempDict = tempArr[0] as Dictionary<String,AnyObject>
var tempString = tempDict["year"]! as String
var tempInt = tempString.toInt()
var year = (((JSONDict["years"]! as [AnyObject])[0] as Dictionary<String,AnyObject>)["year"] as String).toInt()

Thanks in advance!


Solution

  • You are missing ! after ["year"]

     var year = (((JSONDict["years"]! as [AnyObject])[0] as Dictionary<String,AnyObject>)["year"]! as String).toInt()