Is possible to add some padding into an Array?

时间:2016-02-12 21:13:15

标签: ios swift nsarray padding

I was wandering..... Is possible to add some padding into an array? For example: if i have something like # y-range layer_scales(plt, 1, 2)$y$range$range # x-range layer_scales(plt, 1, 2)$x$range$range What kind of data can i put inside the array as padding to obtain an array of 5 elements like var array:[NSDate] = [].

I would like to maintain the position and the index of the elements also if some of them are nil.

Hope I made myself clear.

2 个答案:

答案 0 :(得分:0)

回答其他用户的问题。

var array:[NSDate?] = [nil , nil, NSDate(), nil, nil]
威尔M

[NSDate?]

var dictionary:[Int:NSDate?] = [0 : nil, 1 : nil, 2 : NSDate(), 3 : nil, 4 : nil]
Dan Beaulieu的

[Int:NSDate?]

答案 1 :(得分:0)

答案是肯定的。如果您需要超过5个,您可以轻松自动化该过程。 (您可以将其插入游乐场进行测试。)

func addPaddingTo( var myArray:[AnyObject], intArray:[Int]) -> [AnyObject]{

    //we'll do some checking here to make sure the indices you are changing are in fact valid members of myArray.

    for number in intArray {

        if number < myArray.count {

            myArray[number] = "Padding"
        }
    }

    return myArray
}


var paddedArray = Array < AnyObject >( count: 5, repeatedValue: NSDate())

paddedArray = addPaddingTo(paddedArray, intArray:[1,3,4])