Menu
CoddyTech

Strings and Interpolation

Lesson 6 of 48 in Coddy's Swift for Dart Developers course.

Dart interpolates with $name or ${expression}. Swift uses a backslash followed by parentheses around the expression. Both can concatenate strings, but neither interpolation syntax can be copied unchanged into the other language. Swift String is a value type and its text operations use Unicode-aware indices, not Dart integer offsets. An equality test compares text; interpolation creates new text. This example builds a log message from an event count and a Boolean so you can see how their textual forms enter the message.

Complete Dart program. Each labelled block is a separate file.

main.dart

void main() {
  final retries = 2;
  print('attempts=${retries + 1}; ready=${retries < 4}');
}

Complete Swift program. Each labelled block is a separate file.

main.swift

import Foundation
let retries = 2
print("attempts=\(retries + 1); ready=\(retries < 4)")
challenge icon

Challenge

Easy

Implement badge(_:team:). Input is two ASCII words of 1 to 20 letters: first name then team. Output the team in square brackets, one space, then the name.

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 {
return badge("Ivo", team: "Oak") == "[Oak] Ivo" && badge("Lee", team: "Pine") == "[Pine] Lee"
}
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 Dart Developers

Practice on your own: Swift playground