Functions Returning Tuples in Swift
📖 Scenario: You are building a simple app that processes information about a book. You want to create a function that returns multiple pieces of information about the book at once.
🎯 Goal: Build a Swift function that returns a tuple containing the title and the number of pages of a book. Then, use this function to get the book details and print them.
📋 What You'll Learn
Create a function named
getBookInfo that returns a tuple with two values: a String called title and an Int called pages.Call the
getBookInfo function and store its result in a variable named bookDetails.Extract the
title and pages from the tuple stored in bookDetails.Print the book title and number of pages using the extracted values.
💡 Why This Matters
🌍 Real World
Functions returning tuples are useful when you want to return multiple related pieces of information from a function without creating a custom data type.
💼 Career
Understanding tuples helps in writing clean and efficient Swift code, which is important for iOS app development and other Swift-based projects.
Progress0 / 4 steps