How to create array types?

I've just started learning Swift and been messing around with arrays. I can't seem to find a way to create an array type. All examples I've seen declare an array variable that's empty or has been initialised. Can I create a one dimensional array type of say 9 integers, then declare 4 variables of this initialised differently? When I was trying to do a 2d array I thought I could create an array of arrays (I've since learnt the correct syntax, but still).


Could I create a 3d array with the first 2 dimensions being static to create a 5x5 grid and then a dynamic array for the 3rd dimension?


I've used C, C++, C# etc previous to this so used to defining my own types.


Thanks,
Graham

Swift arrays don't have a size argument. You just define an array, then append/remove as many elements as you need. You can use the reserveCapacity method to reserve contiguous storage space, but you can still have more or fewer elements than that in the array. You could create a structure (value type) or class (reference type) that contains a read-only array property, a size property, and an append method that limits how many items can be appended.

Thanks for the reply.


I did think about the struct/class idea, so that's encouraging.


While we're at it, is there a way to change the size of an array? I've come across removeRange, but I was thinking of just changing the size. So if I had a 5x5x5 cube I could just change the size of the third dimension to 1 and have a 5x5x1 grid. If my third dimension changed, using removeRange would need to check the current range to ensure it removed everything or indeed didn't try to remove something that wasn't there.

You could make the size property of your struct/class a computed property (get/set), or use a property observer (willSet/didSet), and create/verify the range to remove (and then remove it) in the setter or the willSet observer.

How to create array types?
 
 
Q