Greetings, traveler!
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.
Think of InlineArray
as the best of both worlds:
- Like tuples, it has a predefined size known at compile time.
- Like arrays, it supports subscript access to elements.
- And unlike both, it brings performance optimizations thanks to its predictable layout.
What Problem Does InlineArray
Solve?
Standard arrays in Swift are dynamic. They allow appending, removing, resizing, and copying — which is great for flexibility but not ideal for predictability and performance-critical code (like in real-time rendering or signal processing).
On the other hand, tuples give you fixed-size storage, but they’re awkward to work with because they lack indexing, iteration, or any collection-like behavior.
InlineArray
solves this by providing a fixed-size buffer with subscript access and predictable memory usage, without the overhead of dynamic memory allocation.
Creating an InlineArray
You can define an InlineArray
in a few ways, thanks to integer generics (introduced in SE-0452):
var scores: InlineArray<3, Int> = [85, 90, 95]
Or let Swift infer the size and type based on the initializer:
var colors: InlineArray = ["Red", "Green", "Blue"]
Both examples above create arrays of exactly three elements. This size is enforced by the compiler, so the following would not compile:
var tooMany = InlineArray<3, Int>([1, 2, 3, 4]) // ❌ Error
Reading and Writing Values
Although you can’t use .append()
or .remove(at:)
, you can still access and modify individual elements:
scores[1] = 93
print(scores[1]) // 93
You can iterate over them using the indices
property:
for index in scores.indices {
print("Score \(index): \(scores[index])")
}
When Should You Use InlineArray
?
This structure is ideal when:
- You need guaranteed memory layout for performance (e.g. low-level computation, graphics, DSP).
- You want predictability — no resizes, no dynamic memory allocations.
- You’re dealing with a fixed number of values (like RGB colors, matrix elements, or constants in a shader pipeline).
A Note on Limitations
InlineArray
does not conform toSequence
orCollection
, which means you can’t usefor in
directly or methods likemap()
orfilter()
.- It’s intentionally minimal — no dynamic resizing or automatic memory management — making it perfect for predictable, low-level code.
- It’s not a replacement for
Array
, but a complement for specific use cases.
Looking Ahead
A related proposal, SE-0483, suggests adding custom literal syntax like this:
var scrores: [5 x Int] = .init(repeating: 0)
Summary
InlineArray
in Swift 6.2 is a niche but powerful tool:
- Fixed-size
- Subscriptable
- High-performance
- Great for memory-sensitive or low-level code
It’s another step in Swift’s evolution toward offering more control without giving up clarity.
If you enjoyed this article, please feel free to follow me on my social media:
It might be interesting:
- Leveraging Enums for Flexible Button Styling in SwiftUI
- How to access UIHostingController from a SwiftUI View
- Simplifying Data Access in SwiftUI with @dynamicMemberLookup
- Three Practical Tools for Managing References and Detecting Memory Leaks in Swift
- Paging with Peek: Three Ways to Implement Paginated Scroll in SwiftUI