I have an Int that I saved in Parse as an AnyObject. When I retrieve the AnyObject? and try to cast it as a String, NSString, NSNumber, or anything else, I keep getting an EXC_Breakpoint as the casting is returning Nil and there's a "Swift dynamic cast failed" error".
I tried to create this simple test to figure out which part fails, but the crazy thing is that this test will pass where seemingly all the steps are the same:
func testAnyObjectCasting(){
var myInt32: Int32 = 265
var myInt: Int = Int(myInt32)
var myAnyObject: AnyObject? = myInt as AnyObject
var myAnyArray: [[AnyObject]] = [[AnyObject]]()
myAnyArray.append(["something", myAnyObject!])
println(myAnyObject)
var myOtherAnyObject: AnyObject? = myAnyArray[0][1]
println(myOtherAnyObject)
var myString:NSNumber? = myOtherAnyObject as? NSNumber
println(myString)
var myInt2: Int = myString! as Int
}
Here's the relevant code snippets from my logic, and notes that println() works fine until the downcast to NSNumber, at which time Nil is returned:
//ABRecordGetRecordId returns an ABRecordID, which is of type Int32
//This value that's stored in the 2nd column of the multiDim [[AnyObject]]
var personId: Int = Int(ABRecordGetRecordID(person))
//This is a call to a Parse object, which returns an AnyObject. I then cast that to
//a multidimensional array AnyObject as that's the true structure of the data in swift speak
var deviceContacts: [[AnyObject]] = device?[deviceContactsFieldName] as [[AnyObject]]
//This returns the expected value, which in my broader test case is 99999, which is supported by Int
var i:Int = 1
println("the value in device contacts \(i) column 1 is: \(deviceContacts[i][1])")
//This takes a single cell value from the multidim array and puts it in an optional AnyObject
var valueInParse: AnyObject? = deviceContacts[i][1]
//This still returns 99999
println(valueInParse)
//This is where 99999 is replaced with nil. Any ideas?
var valueNSNumberInParse: NSNumber? = valueInParse as? NSNumber
//Nil :(
println(valueNSNumberInParse)
//Exception as I'm trying to unwrap nil :(
var unwrappedNSNumber:NSNumber = valueNSNumberInParse!
Part of my frustration is that I don't understand why println() works fine for AnyObject but all the casting fails. Clearly there's code that can interpret the value as a string to show for println, but that syntax eludes me for proper casting.