Tuples and Multiple Results
Lesson 18 of 48 in Coddy's Swift for C Developers course.
C can group several return values in a struct or write through output pointers. Swift can return a tuple with labels without declaring a separate nominal type. Tuple labels describe components but do not introduce a new named model type with its own methods.
The source returns a FileInfo struct by value, including a pointer to a string literal with static storage. Swift returns a tuple containing an owned String value and Bool. Callers can access tuple labels or destructure into locals. A copied C pointer field does not clone the referenced text.
C: complete program
file_info.h
#ifndef FILE_INFO_H
#define FILE_INFO_H
#include <stdbool.h>
typedef struct { const char *suffix; bool visible; } FileInfo;
static FileInfo describe(void) { return (FileInfo){ "csv", true }; }
#endif
main.c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "file_info.h"
int main(void) {
FileInfo info = describe();
printf("%s %s\n", info.suffix, info.visible ? "true" : "false");
return 0;
}
Swift: complete program
Functions.swift
import Foundation
func describe() -> (suffix: String, visible: Bool) {
(suffix: "csv", visible: true)
}main.swift
import Foundation
let info = describe()
print("\(info.suffix) \(info.visible)")Use a Swift struct when the result needs reusable behavior or a domain identity. A small temporary grouping can remain a tuple.
Challenge
EasyImplement 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") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for C Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry RulesPractice on your own: Swift playground