Hey guys im very new to swift and im trying to loop through an NSArray imported from a plist which I then want to populate a 2D swift array with a list of CGPoints.
My plist is set out for with each letter of the Alphabet, within each letter I have an array[][] first row is strokes and second row then holds x,y coordinates.
My variables:
var letterPoints: NSDictionary?
var letterPointsArray: NSArray?
var letterCGPoints = [[Any]]()
My code:
letterPoints = NSDictionary(contentsOfFile: Bundle.main.path(forResource: "LetterPoints", ofType: "plist")!)
letterPointsArray = (letterPoints?.value(forKey: image)) as? NSArray
var i = 0
for strokes in letterPointsArray! as! [[Int]]
{
for points in strokes
{
let x = strokes[i][0] // Error Value of type 'Int' has no subscripts.
let y = strokes[i][1]
letterCGPoints[i].append(CGPoint(x: x,y: y))
}
i = i+1
}
Im getting an error when assigning x and y - Value of type 'Int' has no subscripts.
I originally had this working when I had it set up without the extra row for each stroke but after turning it into a 2D array I'm now struggling to work with it and am quite lost finding the correct way of going about it from my online searches and looking through stackoverflow. Please excuse my ignorance and complete lack of knowledge in swift, would really appreciate some advice on the best way solve this. Thanks:)
Declare your array correctly from the start
var letterPointsArray: [[[Int]]]?
and then cast it directly
letterPointsArray = (letterPoints?.value(forKey: "image")) as? [[[Int]]]
or even better in one line
let letterPointsArray = (letterPoints?.value(forKey: "image")) as? [[[Int]]]
And by using enumerated()
you can then iterate over the array
if let letterPointsArray = (letterPoints?.value(forKey: "image")) as? [[[Int]]] {
for (i, strokes) in letterPointsArray.enumerated() {
Check your strokes array. The following code might work.
let points: [[Int]] = [[10, 20], [30, 40]]
var i = 0
var j = 1
for point in points {
let a = point[i]
let b = point[j]
print(a,b)
}
i+=1
j+=1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.