Reputation: 168
I am trying to make a loop in my code like this:
for row in rows! {
print("Row",row)
let pin = Pin.init(latitude: row[0] as! Float, longitude: row[1] as! Float, pinType: row[2] as! String, beaconID: row[3] as! Int, altitude: row[4] as! Float)
pinList.append(pin)
}
Here, row is an Any
and I'm creating the pin object based on the row's values.
Here is my Pin Class:
class Pin {
var latitude:Float
var longitude:Float
var pinType:String
var beaconID:Int
var altitude:Float
init(latitude:Float, longitude:Float, pinType:String, beaconID:Int, altitude:Float){
self.latitude = latitude
self.longitude = longitude
self.pinType = pinType
self.beaconID = beaconID
self.altitude = altitude
}
}
But I got this error:
Could not cast value of type '
NSTaggedPointerString
' (0x10146ecd8) to 'NSNumber
' (0x102675600).
while I am trying to create the Pin object.
Can anyone help me to fix this problem? Thanks.
Upvotes: 3
Views: 9557
Reputation: 409
id = Int((content[0]["id"] as! NSString).floatValue)
This works fine for me. Content is json array
Upvotes: 11
Reputation: 168
Unexpectedly, I found the answer by reading along the many documentations. Here is the solution for this problem.
for row in rows! {
print("Row",row)
let lat = (row[0] as! NSString).floatValue
let lng = (row[1] as! NSString).floatValue
let pType = (row[2] as! NSString) as String
let bID = (row[3] as! NSString).integerValue
let alti = (row[4] as! NSString).floatValue
let pin = Pin.init(latitude: lat, longitude: lng, pinType: pType, beaconID: bID, altitude: alti)
pinList.append(pin)
}
I tried to convert all the values from row to NSString first and convert the result to the types that I really convert to. :)
Upvotes: 0