Reputation: 668
I'm evaluating swiftyJSON by trying something simple first and wondering what is the issue with this loop.
Here's sample data.
{
"dataplot2d": [
[0.25,2.3 ],
[-2.5,8.09 ],
[5.3145,20.205]
]
}
and here is code to read it into two separate arrays
var x:[Float] = []
var y:[Float] = []
var i:Int = 0
var t:Int = 0
var jsonArr = json["dataplot2d"].arrayValue
for ( i=0; i<jsonArr.count; i++ )//number of data points
{
println("i \(i) t \(t) \(jsonArr[i][t])")
x.append(jsonArr[i][0].array)
y.append(jsonArr[i][1].array)
}
The error is
Could not find member 'array'
??
array
is defined https://github.com/SwiftyJSON/SwiftyJSON for arrayValue
//Non-optional [JSON]
public var arrayValue: [JSON] {
get {
return self.array ?? []
}
}
What am I missing?
Maybe this would be cleaner without swiftyJSON??
Upvotes: 0
Views: 384
Reputation: 70098
Note: I'm transforming my comment into an answer so I can post code.
In your code, jsonArr
is your first-level array of arrays, so jsonArr[i]
is an array of floats, and jsonArr[i][0]
is a float, not an array. That was the source of the confusion. :)
As for the simplicity of the code, here's the same loop as yours but with a slightly "Swifter" syntax (I had to adapt the code a bit to make it work in a Playground, but it doesn't change anything to our matter):
var x:[Float] = []
var y:[Float] = []
let jsonArr: [[Float]] = [[0.25,2.3 ],[-2.5,8.09 ],[5.3145,20.205]]
for floatArray in jsonArr {
x.append(floatArray[0]) // In your case, floatArray[0].float
y.append(floatArray[1])
}
Upvotes: 1
Reputation: 668
Simply needed to unwrap to float, too bad this isn't really documented anywhere.
x.append(jsonArr[i][0].array)
y.append(jsonArr[i][1].array)
is
x.append(jsonChart[i][0].float! )
y.append(jsonChart[i][1].float! )
Upvotes: 0