I wanted to filter the entire array just by the first 4 characters of [i].name

You can achieve this by filtering the array based on the substring value of the name, as follows:

let filteredArray = collectionArray.filter {
    $0.name.substring(to: $0.name.index($0.name.startIndex, offsetBy: 4)).lowercased() == "pk00"
    // or instead of "pk00", add the first 4 characters you want to compare
}

filteredArray will be filled based on what is the compared string.

Hope this helped.

Answer from Ahmad F on Stack Overflow
🌐
Hacking with Swift
hackingwithswift.com › plus › functional-programming › transforming-data-with-map
Transforming data with map() – Hacking with Swift+
At a broader level the two are quite different because the Swift compiler has special knowledge of how Optional works so that things like if let and guard let can work, but the actual underlying types for both are just a plain old enum. Both of these contain data somehow, and so both have a built-in map() method that extracts their data, transforms them using a function we specify, and puts them back into a container.
🌐
ZeroC
doc.zeroc.com › ice › 3.7 › language-mappings › swift-mapping › client-side-slice-to-swift-mapping › swift-mapping-for-structures
Swift Mapping for Structures - Ice
For example: ... public struct ... Int16) { self.hour = hour self.minute = minute self.second = second } } ... A Slice structure with a data member of a class type is mapped to a Swift class....
People also ask

How to define a map in Swift?
In Swift, you can create a map (also known as a dictionary) by using the following syntax: ```swift var map = [KeyType: ValueType] ``` Where KeyType is the type of value that can be used as a dictionary key, and ValueType is the type of values that the dictionary holds.
🌐
dhiwise.com
dhiwise.com › post › exploring-the-intricacies-of-swift-map-dictionary
The Ultimate Swift Map Dictionary Tutorial for Beginners
What is mapValues in Swift dictionary?
The mapValues function in Swift is used to transform the values in a dictionary. Unlike the map function, mapValues returns a new dictionary with transformed values, and the original keys unchanged.
🌐
dhiwise.com
dhiwise.com › post › exploring-the-intricacies-of-swift-map-dictionary
The Ultimate Swift Map Dictionary Tutorial for Beginners
How to map dictionary to model in Swift?
You can map a dictionary to a model in Swift by looping over the dictionary and assigning each key-value pair to a property in the model.
🌐
dhiwise.com
dhiwise.com › post › exploring-the-intricacies-of-swift-map-dictionary
The Ultimate Swift Map Dictionary Tutorial for Beginners
🌐
Cocoa Casts
cocoacasts.com › swift-essentials-1-how-to-use-swift-map-to-transforms-arrays-sets-and-dictionaries
How to Use Swift Map to Transform Arrays, Sets, and ...
import Foundation let strings = Set([ "one", "two", "three" ]) let ints = strings.map { $0.count } print(ints) Run the contents of the playground to see the result. You can verify that the order of the elements is undefined if you run the contents of the playground several times. It should be no surprise that Swift's map(_:) method can also be used to transform the keys and values of a dictionary.
🌐
Codecademy
codecademy.com › learn › intro-to-algorithms-and-linear-data-structures-in-swift › modules › hash-tables-swift › cheatsheet
Introduction to Algorithms and Linear Data Structures in Swift: Hash Tables Cheatsheet | Codecademy
Each index in the array can store one key-value pair. If the hash map is implemented using chaining for collision resolution, each index can store another data structure such as a linked list, which stores all values for multiple keys that hash to the same index.
🌐
Medium
nitinagam.medium.com › data-structure-in-swift-ios-part-1-305dd33e19f5
Data Structure in Swift (Array & Dictionary) — Part 1 | by Nitin Aggarwal | Medium
February 13, 2020 - In this story, we will learn about the basic data structures in the Swift language i.e. Array and Dictionary and will cover the things…
🌐
Medium
mehrdad-ahmadian.medium.com › understanding-map-flatmap-and-compactmap-in-swift-eacafc38fb61
Understanding map, flatMap, and compactMap in Swift | by Mehrdad Ahmadian | Medium
December 24, 2023 - Understanding when and how to use ... and efficiency. map is your go-to for simple transformations, flatMap for dealing with nested collections or optionals, and compactMap for filtering out unwanted nil values....
Find elsewhere
🌐
DhiWise
dhiwise.com › post › exploring-the-intricacies-of-swift-map-dictionary
The Ultimate Swift Map Dictionary Tutorial for Beginners
July 10, 2024 - Understanding how to create and manipulate these key-value pairs in a Swift Map Dictionary is crucial for effective iOS programming. A dictionary’s keys and values may be of any type as long as they are hashable, meaning their values must maintain their integrity while stored. As the name implies, key-value pairs link a unique key to its corresponding value, forming the basic structure of a Swift dictionary.
🌐
Bugfender
bugfender.com › blog › swift-arrays
Swift Arrays: Map, Filter, Reduce & Sort Explained | Bugfender
November 7, 2025 - In fact, they’re among the most powerful and versatile data structures in our toolkit. Swift arrays were introduced way back in 2014, but many developers find them confusing – particularly those who are used to reference types, or don’t fully grasp the possibilities that arrays provide. So in this guide, we’ll go beyond the basics and explore advanced Swift array operations like map, filter, reduce, and sorted, with practical examples and performance notes.
🌐
Swift Forums
forums.swift.org › evolution › discussion
Adding more data structures to the standard library - Discussion - Swift Forums
April 25, 2019 - By far the most painful part of ... state of data structures in Swift. If I were programming in Java (which I really rather wouldn't), I would have easy access to... Java's collections Sets Unordered sets (HashSet, CopyOnWriteArraySet, EnumSet) Insertion ordered sets (LinkedHashSet) Naturally ordered sets (TreeSet, ConcurrentSkipListSet) Maps ("Dictionaries" ...
🌐
Pluralsight
pluralsight.com › blog › guides
Data Structures in Swift - Part 1 | Online Courses, Learning Paths, and Certifications - Pluralsight
Dictionaries - also known as hash maps - store key-value pairs and allow for efficient setting and reading of values based on their unique identifiers. Just like the other Swift collection types, the Dictionary is also implemented as a generic type, meaning that it can take on any data type.
🌐
Medium
medium.com › nsistanbul › object-mapping-in-swift-d9cd7ecd47dd
Object Mapping in Swift. Hi all, | by alicanbatur | NSIstanbul | Medium
December 3, 2017 - Let’s start from the worst way, the non-mapping way :) We have a person struct in the example above. First, we will add an init method to it. init(with dictionary: [String: Any]?) { guard let dictionary = dictionary else { return } name = dictionary["name"] as? String surname = dictionary["surname"] as? String age = dictionary["age"] as? Int } ... var data: Data = JSON.data(using: .utf8)!
🌐
Andy Ibanez
andyibanez.com › posts › understanding-basic-data-structures-dictionaries-in-depth
Understanding Basic Data Structures in Swift: Dictionaries in Depth - Andy Ibanez
January 27, 2021 - Whether you are a seasoned developer with a lot of code out in the wild world, or you started learning programming this week, chances are you hace used (and seen) dictionaries being used in many places. Also known as hashmaps or hash tables, dictionaries allow us to store key-value mappings, from one object to another. In this article we will study this structure which is known by everyone, and we will also learn about its quirks and unknown features.
🌐
Swift by Sundell
swiftbysundell.com › articles › picking-the-right-data-structure-in-swift
Picking the right data structure in Swift | Swift by Sundell
Since each kind of data structure ... becoming. The Swift standard library ships with three main data structures — Array, Dictionary and Set — that each comes with a different set of optimizations, pros and cons...
Top answer
1 of 4
7

There are exactly two cases where the Swift compiler infers the return type of a closure automatically:

  • In a "single-expression closure," i.e. the closure body consists of a single expression only (with or without explicit closure parameters).
  • If the type can be inferred from the calling context.

None of this applies in

let inActionPersons = persons.map { (var p) in
    p.active = false
    return p
}

or

let a = persons.map { p in
        var p1 = p
        p1.active = false
        return p1
}

and that's why you have to specify the return type explicitly as in Kametrixom's answer.

Example of a single-expression closure:

let inActionPersons = persons.map { p in
    Person(firstName: p.firstName, lastName: p.lastName, active: false)
}

and it would compile with (var p) in or (p : Person) in as well, so this has nothing to do with whether the closure arguments are given explicitly in parentheses or not.

And here is an example where the type is inferred from the calling context:

let a : [Person] = persons.map { p in
    var p1 = p
    p1.active = false
    return p1
}

The result of map() must be a [Person] array, so map needs a closure of type Person -> Person, and the compiler infers the return type Person automatically.

For more information, see "Inferring Type From Context" and "Implicit Returns from Single-Expression Closures" in the "Closures" chapter in the Swift book.

2 of 4
6

When using the brackets for arguments so that var works, you have to put the return type as well:

let inActionPersons = persons.map { (var p) -> Person in

    p.active = false
    return p
}
🌐
Dhiwise
dhiwise.com › post › sequencing-success-a-dive-into-swift-map-for-efficient-coding
Mastering Swift Map Functions: A Comprehensive Guide
Simplified Data Transformation: With Swift Map, developers can effortlessly transform arrays, strings, and other data structures by applying specified transformations to individual elements.
🌐
Firebase
firebase.google.com › documentation › firestore › map cloud firestore data with swift codable
Map Cloud Firestore data with Swift Codable | Firebase
(Optional, if you want to use the type in a List view) Add an id property to your type, and use @DocumentID to tell Cloud Firestore to map this to the document ID. We'll discuss this in more detail below. Use documentReference.data(as: ) to map a document reference to a Swift type.
🌐
Swift by Sundell
swiftbysundell.com › basics › map-flatmap-and-compactmap
Map, FlatMap and CompactMap | Swift by Sundell
One way of performing such value transformations is by mapping a collection of values into an array of new values, using a transform. The Swift standard library offers three main APIs for that kind of mapping — map, flatMap and compactMap.