Because 2D arrays in swift can have subarrays with different lengths. There is no "matrix" type.

let arr = [
    [1,2,3,4,5],
    [1,2,3],
    [2,3,4,5],
]

So the concept of "rows" and "columns" does not exist. There's only count.

If you want to count all the elements in the subarrays, (in the above case, 12), you can flat map it and then count:

arr.flatMap { $0 }.count

If you are sure that your array is a matrix, you can do this:

let rows = arr.count
let columns = arr[0].count // 0 is an arbitrary value
Answer from Sweeper on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @gvardikos โ€บ dynamic-array-size-in-swift-append-vs-reservecapacity-2ffdd41fef90
What is array reserveCapacity()
July 23, 2023 - In Swift, arrays have a dynamic size by default, meaning you can add or remove elements from an array after it has been created. So if you create an array of four elements, swift will allocate memory enough for the four elements.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ swift-tutorial โ€บ get-array-size-count-in-swift
Get Array Size in Swift - count function - Examples
July 23, 2026 - Swift arrays do not require a fixed length when declared. To create an array that initially contains a specific number of identical values, use Array(repeating:count:). ... The array starts with five elements, but it remains mutable and can ...
Discussions

Determine Size of Multidimensional Array in Swift - Stack Overflow
I am new to Swift and am struggling to work out how to determine the size of a multidimensional array. I can use the count function for single arrays, however when i create a matrix/multidimensio... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Fixed size array hacks - Using Swift - Swift Forums
Swift doesn't have fixed size arrays, and I guess many of us have experimented with various workarounds. I thought it would be fun to share and discuss our ways around Swift's lack of fixed size arrays here. From the very simple ones like: var myFixedSizeArrayOfEightInts = (0, 0, 0, 0, 0, 0, ... More on forums.swift.org
๐ŸŒ forums.swift.org
3
January 23, 2020
Is it possible to limit the size of an array if the user isn't a subscriber
Yes but I'd recommend you do yourself a favor and do some introductory computer science learning. A question like this means you've started experimenting with apps with very little knowledge of programming/data fundamentals. It's great you're creating an app, but you will have much better luck if you learn the basics first. More on reddit.com
๐ŸŒ r/swift
8
0
October 22, 2022
Limit array size in swift - Stack Overflow
Is there a way to limit array size in swift so that when you append elements to it when it's full, it doesn't append? I know this can be done programmatically. Wondering if swift has inbuilt handl... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Programiz
programiz.com โ€บ swift-programming โ€บ library โ€บ array โ€บ count
Swift Array count (With Examples)
// true because there are 10 elements on numbers if (numbers.count > 5) { print( "The array size is large") } else { print("The array size is small") }
๐ŸŒ
Swift Forums
forums.swift.org โ€บ using swift
Fixed size array hacks - Using Swift - Swift Forums
January 23, 2020 - Swift doesn't have fixed size arrays, and I guess many of us have experimented with various workarounds. I thought it would be fun to share and discuss our ways around Swift's lack of fixed size arrays here.
๐ŸŒ
Livsy Code
livsycode.com โ€บ home โ€บ swift โ€บ inlinearray โ€“ a fixed-size arrays in swift
InlineArray โ€“ A Fixed-Size Arrays in Swift โ†’ Livsy Code
May 13, 2025 - Swift 6.2 introduces a powerful new data structure: InlineArray. If youโ€™ve ever needed an array with a fixed size, optimized for performance, but found tuples too clunky and standard arrays too flexible โ€” this feature is for you.
Find elsewhere
๐ŸŒ
Gitbooks
victorqi.gitbooks.io โ€บ swift-algorithm โ€บ content โ€บ fixed_size_array.html
Fixed Size Array ยท Swift Algorithm - victorqi
It will always be this size. If you need to fit more than 10 elements, you're out of luck... there is no room for it. To get an array that grows when it gets full you need to use a dynamic array object such as NSMutableArray in Objective-C or std::vector in C++, or a language like Swift whose ...
๐ŸŒ
Ole Begemann
oleb.net โ€บ blog โ€บ 2017 โ€บ 12 โ€บ fixed-size-arrays
A hack for fixed-size arrays in Swift โ€“ Ole Begemann
Swift doesnโ€™t have a built-in type for fixed-size, stack-allocated arrays. The standard library uses code generation to work around this limitation for internal use.
Top answer
1 of 5
8

Nope, Swift doesn't come with this kind of array - which is similar to a View from a database - allowing you to peek the first N elements. Though for this both the view and it target should be reference types, which is not the case in Swift for arrays.

But enough blabbing around, you could quickly write a wrapper over an Array for fulfilling your needs:

/// an array-like struct that has a fixed maximum capacity
/// any element over the maximum allowed size gets discarded
struct LimitedArray<T> {
    private(set) var storage: [T] = []
    public let maxSize: Int

    /// creates an empty array
    public init(maxSize: Int) {
        self.maxSize = maxSize
    }

    /// takes the max N elements from the given collection
    public init<S: Sequence>(from other: S, maxSize: Int) where S.Element == T {
        self.maxSize = maxSize
        storage = Array(other.prefix(maxSize))
    }

    /// adds a new item to the array, does nothing if the array has reached its maximum capacity
    /// returns a bool indicated the operation success
    @discardableResult public mutating func append(_ item: T) -> Bool {
        if storage.count < maxSize {
            storage.append(item)
            return true
        } else {
            return false
        }
    }

    /// inserts an item at the specified position. if this would result in
    /// the array exceeding its maxSize, the extra element are dropped
    public mutating func insert(_ item: T, at index: Int) {
        storage.insert(item, at: index)
        if storage.count > maxSize {
            storage.remove(at: maxSize)
        }
    }

    // add here other methods you might need
}

// let's benefit all the awesome operations like map, flatMap, reduce, filter, etc
extension LimitedArray: MutableCollection {
    public var startIndex: Int { return storage.startIndex }
    public var endIndex: Int { return storage.endIndex }

    public subscript(_ index: Int) -> T {
        get { return storage[index] }
        set { storage[index] = newValue }
    }

    public func index(after i: Int) -> Int {
        return storage.index(after: i)
    }
}

Since the struct conforms to Collection, you can easily pass it to code that knows only to work with arrays by transforming its contents into an array: Array(myLimitedArray).

2 of 5
3

simply count the elements of array before appending new element.

var array :[Int] = [1,2,3,4,5,6,7,8,9,10]

func insertIntoArray(_ value: Int, array: [Int]) -> [Int] {
    var arr = array
    if arr.count == 10 {
        arr.removeLast()
    }
    arr.append(value)
    return arr
}

array = insertIntoArray(11, array: array)
๐ŸŒ
Medium
somestay.medium.com โ€บ deep-dive-into-swifts-array-ed82b88dca08
Deep dive into Swift's array - Timur Cheberda
June 6, 2022 - Thus, when you add 11 and 12 elements to an array with a capacity of 10, swift creates an array with a size of 20.
๐ŸŒ
GitHub
gist.github.com โ€บ CTMacUser โ€บ cfffa526b971d0e1f3a079f53c6819bb
Swift proposal for fixed-size array types ยท GitHub
Proposal: Contiguous Variables (A.K.A. Fixed Sized Array Type) ... [swift 4] static libs/modular code, fixed-size arrays, ref/pointer to structs, pointers, numeric types.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ swift โ€บ swift_arrays.htm
Swift - Arrays
Swift puts strict checking which does not allow you to enter a wrong type in an array, even by mistake. If you assign a created array to a variable, then it is always mutable, which means you can change it by adding, removing, or changing its elements; but if you assign an array to a constant, then that array is immutable, and its size ...
๐ŸŒ
Medium
medium.com โ€บ ios-ic-weekly โ€บ size-and-stride-basics-of-memory-layout-in-swift-b3e48cb9e34d
Size and Stride โ€” Basics of Memory Layout in swift | by Xiangyu | iOS IC Weekly | Medium
November 15, 2024 - For example, if you have an array of 10 Int values on a 64-bit system, and each Int occupies 8 bytes, the total size is 10 * 8 = 80 bytes. The stride refers to the number of bytes you need to move in memory to get from one element to the next.
๐ŸŒ
Swift Forums
forums.swift.org โ€บ evolution โ€บ discussion
Approaches for fixed-size arrays - Discussion - Swift Forums
July 12, 2022 - We're looking into how best to give Swift the equivalent functionality of fixed-size arrays in C or other systems languages. Here are some directions I've been considering, and I'd love to hear the community's ideas and reactions: Tuples as fixed-size arrays This was the approach outlined in our most recent pitch on the subject: we already use homogeneous tuples to represent imported fixed-size array fields from C, and library developers sometimes reach for them manually (usually under duress) ...