Misc
Handy tips and useful features in Swift.
The print() Function
You can pass multiple items to a single print() function by separating them with commas.
swift
print("Ready", "Set", "Go")
// prints "Ready Set Go"Separator
You can change the text inserted between items by using the separator parameter.
swift
print("Apple", "Banana", "Cherry", separator: " & ")
// prints "Apple & Banana & Cherry"Terminator
By default, print() adds a newline. You can change this behavior by using the terminator parameter.
swift
print("Hello", terminator: " ")
print("World")
// prints "Hello World"Comments
| Type | Syntax | Use Case |
|---|---|---|
| Single-line | // | Brief notes. |
| Multi-line | /* ... */ | Longer explanations. |
| Documentation | /// | Xcode tooltips and Quick Help. |
NOTE
Documentation comments (///) are highly recommended for public APIs and complex logic as they automatically integrate with the Xcode's documentation tools.
Result Type
This is a standard type used to handle success or failure in operations that might produce an error.
swift
func checkStatus() -> Result<String, Error> {
// return .success("OK") or .failure(MyError.someIssue)
}