Menu
CoddyTech

Tuples and Multiple Results

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

GDScript can return several related results in an Array, Dictionary or custom object. This source uses named dictionary entries rather than claiming a built-in typed tuple return. Swift can return a labeled tuple with statically described component types without declaring a separate nominal model.

Swift tuple labels let callers read detail.label and detail.size, or destructure the tuple into local names. These labels do not create a new class or automatically define serialized field metadata. A named struct is useful when the grouping needs lasting invariants or behavior.

The examples uppercase an ASCII stem and count its units. GDScript String length counts code points while Swift count measures Characters; ASCII makes their numerical results agree here.

Complete GDScript comparison program:

extends SceneTree
func describe(stem: String) -> Dictionary:
    return {"label": stem.to_upper(), "size": stem.length()}

func _init():
    var detail = describe("map")
    print(detail["label"], " ", detail["size"])
    quit()

Complete Swift comparison program, compiled from these files:

Functions.swift

func describe(_ stem: String) -> (label: String, size: Int) {
    return (stem.uppercased(), stem.count)
}

main.swift

let detail = describe("map")
print(detail.label, detail.size)
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 GDScript Developers

Practice on your own: Swift playground