I have the following code to read in a JSON file and then cast its arrays to either [Float32] or [Int32]
let location = NSString(string:"/Users/myname/Documents/test.js").stringByExpandingTildeInPath
let rawData = NSData(contentsOfFile: location)
let modelData = try? NSJSONSerialization.JSONObjectWithData(rawData!, options: NSJSONReadingOptions.AllowFragments)
let dict = modelData as! NSDictionary
guard let vertices = dict["vertices"] as? [Float32] else {
print("error casting vertices")
return
}
guard let indices = dict["faces"] as? [Int32] else {
print("error casting indices")
return
}
The contents of the JSON file is:
{
"vertices" : [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9],
"faces" : [0,1,2]
}
The JSON file has been loaded and parsed successfully, however, the cast of vertices
to [Float32]
succeeds while the cast of faces
to [Int32]
fails. Is there anyway to cast faces
to [Int32]
?