Uplift iOS Interview
"Uplift iOS Interview" is a comprehensive guide to help aspiring iOS developers soar to new heights in their careers. This book is an indispensable tool for anyone looking to crack the iOS interview and impress their future employers with their technical prowess. With in-depth coverage of Swift, AutoLayout, SwiftUI, Multithreading, Memory management so on and so forth, this book is a treasure trove of knowledge for anyone looking to uplift their iOS development career.
In Swift, there are several ways to convert one array of objects to another array of objects. One way is to use a for-in
loop and map the elements of the original array to the new array. The map
function allows you to apply a transformation to each element of an array and returns a new array with the transformed elements.
struct Person { let firstName: String let lastName: String } let people = [Person(firstName: "John", lastName: "Doe"), Person(firstName: "Jane", lastName: "Smith")] let fullNames = people.map { "\($0.firstName) \($0.lastName)" } print(fullNames) // ["John Doe", "Jane Smith"]
Read more on The power of map in Swift and Map, FlatMap, Filter, Reduce – High order functions in Swift 5+
Another way to convert an array of objects is to use the compactMap
function. This function works similarly to the map
function, but it also filters out nil values.
struct Person { let firstName: String let lastName: String var fullName: String? } let people = [Person(firstName: "John", lastName: "Doe"), Person(firstName: "Jane", lastName: "Smith")] let firstNames = people.compactMap { $0.firstName } print(firstNames) // [John, Jane]
Another approach is to use the filter
function, this function allows you to filter the elements of an array based on a certain condition and returns a new array with only the elements that match that condition
struct Person { let firstName: String let lastName: String let age: Int } let people = [Person(firstName: "John", lastName: "Doe", age: 30), Person(firstName: "Jane", lastName: "Smith", age: 25), Person(firstName: "Robert", lastName: "Johnson", age: 35)] let adults = people.filter { $0.age >= 18 } print(adults) // [Person(firstName: "John", lastName: "Doe", age: 30), Person(firstName: "Jane", lastName: "Smith", age: 25), Person(firstName: "Robert", lastName: "Johnson", age: 35)]
Lastly, you can also use the reduce
function to convert one array of objects to another array of objects. The reduce
function takes an initial value and a closure that takes two parameters. It applies the closure to the initial value and the first element of the array, then applies the closure to the result and the next element of the array, and so on.
struct Person { let firstName: String let lastName: String } let people = [Person(firstName: "John", lastName: "Doe"), Person(firstName: "Jane", lastName: "Smith")] let initial: [String] = [] let fullNames = people.reduce(initial) { (result, person) in result + ["\(person.firstName) \(person.lastName)"] } print(fullNames) // ["John Doe", "Jane Smith"]
All these methods will help you to convert an array of objects to another array of objects in Swift
Rev Up Your iOS Skills: Take a Dynamic Learning Journey

iOS Career Boost is the ultimate learning journey to elevate your iOS development career through a dynamic blend of visual learning, handy cheat sheets, coding practice materials, and expertly curated tips and tricks
Get Ready to Shine: Mastering the iOS Interview
- Uplift iOS Interview - A Comprehensive Guide to iOS Interview
- Xcode Cheat Sheet for Swift
- Xcode Cheat Sheet for SwiftUI
Enjoying the articles? Get the inside scoop by subscribing to my newsletter.
Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book. Your email address will only be used for the purpose of sending the newsletter and will not be shared with third parties or advertisers. Rest assured that we value your privacy and will not spam your inbox.
Connect with me on
Twitter and LinkedIn and don't hesitate to reach out with any questions about this post. Thank you for reading.