Menu
CoddyTech

Tuples and Multiple Results

Lesson 18 of 48 in Coddy's Swift for C# Developers course.

C# value tuples and Swift tuples group a few values without introducing a full domain type. Field names make a multi-result function easier to read, but tuples are not a substitute for a type with its own invariants and behavior. In Swift, return a tuple type with labels after the arrow and access a component by its label. Destructuring can give local names to the components. This example groups a fixed label with its letter count and reads the components separately. It does not scan a collection for extrema. C# tuple names are compile-time conveniences; avoid treating either language's labels as serialized field metadata.

C#

Complete program file: Program.cs.

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var entry = (name: "oak", letters: 3);
        Console.WriteLine(entry.name);
        Console.WriteLine(entry.letters);
    }
}

Swift

Complete program file: main.swift.

let entry = (name: "oak", letters: 3)
print(entry.name)
print(entry.letters)
challenge icon

Challenge

Easy

Implement bounds(_:) returning a named tuple with minimum and maximum, or nil for an empty array. Input contains 0 through 50 integers -1000 through 1000. Output minimum then maximum separated by one space, or empty.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
guard let b = bounds([6, -4, 9]) else { return false }; return b.low == -4 && b.high == 9 && bounds([]) == nil
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for C# Developers

Practice on your own: Swift playground