Search code examples
iosarraysswiftuibezierpath

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. But swift arrays are so foreign to me, I don't understand at all. I need a 10x10 grid of squares (UIBezierPaths) Iv tried looking at many posts but I can't understand. Could someone please help out and explain the nature of swift arrays, am I missing something? thank you


Solution

  • 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. 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())
    }