I was wandering..... Is possible to add some padding into an array?
For example: if i have something like var array:[NSDate] = []
What kind of data can i put inside the array as padding to obtain an array of 5 elements like array[NSDate, padding, NSDate, padding , padding]
.
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.
The answer is yes you can. And if you need more than 5 you can automate the process easily. (You can plug this into a playground to test it.)
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])