简体   繁体   English

Swift,如何制作 UIBezierPaths 数组?

[英]Swift, How to make an Array of UIBezierPaths?

I can make 1 just fine, and in Objective-C I would just slap a "[10]" on my first declaration and be good to go.我可以做 1 就好了,而在 Objective-C 中,我会在我的第一个声明中加上一个“[10]”,然后就可以了。 But swift arrays are so foreign to me, I don't understand at all.但是 swift 数组对我来说太陌生了,我根本不明白。 I need a 10x10 grid of squares (UIBezierPaths) Iv tried looking at many posts but I can't understand.我需要一个 10x10 的正方形网格(UIBezierPaths)我尝试查看很多帖子,但我无法理解。 Could someone please help out and explain the nature of swift arrays, am I missing something?有人可以帮忙解释一下 swift 数组的性质吗,我错过了什么吗? thank you谢谢你

Does this help?这有帮助吗?

var paths: [[UIBezierPath]] = [[UIBezierPath]]()

for index in 0 ... 9 {
    paths.append([UIBezierPath]())
    for kdx in 0 ... 19 {
        paths[index].append(UIBezierPath())
    }
}

Creates a 2d array of UIBezierPath and then iterates over two loops.创建UIBezierPath的二维数组,然后迭代两个循环。 In the first loop we add a new list of paths and in the second loop we add new paths to these lists.在第一个循环中,我们添加了一个新的路径列表,在第二个循环中,我们将新路径添加到这些列表中。

For a single list of paths just use the inner loop.对于单个路径列表,只需使用内部循环。

var paths: [UIBezierPath] = [UIBezierPath]()
for kdx in 0 ... 9 {
    paths.append(UIBezierPath())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM