【发布时间】:2020-04-16 04:57:54
【问题描述】:
大家好,我对 swift 非常陌生,我正在尝试遍历从 plist 导入的 NSArray,然后我想用 CGPoints 列表填充 2D swift 数组。
我的 plist 是针对字母表的每个字母设置的,在每个字母中我有一个数组 [][] 第一行是笔划,第二行然后保存 x,y 坐标。
我的变量:
var letterPoints: NSDictionary?
var letterPointsArray: NSArray?
var letterCGPoints = [[Any]]()
我的代码:
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
}
分配 x 和 y 时出现错误 - 'Int' 类型的值没有下标。
我最初有这个工作,当我设置它时没有为每个笔划增加额外的行,但是在将它变成一个二维数组之后,我现在正在努力使用它,并且很迷失找到正确的处理方法从我的在线搜索和通过stackoverflow查看。请原谅我的无知和完全缺乏关于 swift 的知识,非常感谢一些关于解决这个问题的最佳方法的建议。谢谢:)
【问题讨论】:
-
将您的代码添加为 text.not screenshots
-
抱歉编辑了帖子。
-
所以
strokes是一个二维数组,points是一个一维数组。那么你为什么要像二维数组一样访问它呢?应该有一个较小的for循环吗? -
因为我还需要循环遍历笔划内的每一组点,并将它们一个一个附加到 CGPoint 数组中。所以每个笔划都会有多个 x,y 值。我仍然认为我需要额外的循环,但它可能更像 points[0] 和 points[1] 但这也给了我同样的错误
-
不要在 Swift 中使用
NSArray、NSDictionary、NSDictionary(contentsOf、value(forKey。并尽可能避免Any。