String Formatting in Swift

import Foundation

struct Point {
    var x: Int
    var y: Int
}

func main() {
    // Swift offers excellent support for string formatting.
    // Here are some examples of common string formatting tasks.

    // Swift uses string interpolation for most formatting tasks
    let p = Point(x: 1, y: 2)
    print("struct1: \(p)")

    // For more complex structures, you can use the CustomStringConvertible protocol
    print("struct2: \(p)")

    // Swift doesn't have a direct equivalent to Go's %#v, but you can create a custom description
    print("struct3: Point(x: \(p.x), y: \(p.y))")

    // To print the type of a value, use String(describing: type(of:))
    print("type: \(String(describing: type(of: p)))")

    // Formatting booleans is straightforward
    print("bool: \(true)")

    // For integers, use string interpolation or NumberFormatter for more control
    print("int: \(123)")

    // This prints a binary representation
    print("bin: \(String(14, radix: 2))")

    // This prints the character corresponding to the given integer
    print("char: \(UnicodeScalar(33)!)")

    // For hex encoding
    print("hex: \(String(456, radix: 16))")

    // For floats, use string interpolation or NumberFormatter
    print("float1: \(78.9)")

    // For scientific notation, use NumberFormatter
    let formatter = NumberFormatter()
    formatter.numberStyle = .scientific
    formatter.exponentSymbol = "e"
    print("float2: \(formatter.string(from: NSNumber(value: 123400000.0))!)")
    
    formatter.exponentSymbol = "E"
    print("float3: \(formatter.string(from: NSNumber(value: 123400000.0))!)")

    // For basic string printing
    print("str1: \"string\"")

    // Swift doesn't have a direct equivalent to Go's %q, but you can create a custom function
    print("str2: \"\\\"\(String(reflecting: "string"))\\\"\"")

    // For hex representation of a string
    print("str3: \("hex this".data(using: .utf8)!.map { String(format: "%02hhx", $0) }.joined())")

    // To print a representation of a pointer
    print("pointer: \(Unmanaged.passUnretained(p as AnyObject).toOpaque())")

    // For width-specific formatting, use String(format:)
    print("width1: |" + String(format: "%6d|%6d", 12, 345))

    // For float precision
    print("width2: |" + String(format: "%6.2f|%6.2f", 1.2, 3.45))

    // For left-justified formatting
    print("width3: |" + String(format: "%-6.2f|%-6.2f", 1.2, 3.45))

    // For string width formatting
    print("width4: |" + String(format: "%6s|%6s", "foo", "b"))

    // For left-justified string formatting
    print("width5: |" + String(format: "%-6s|%-6s", "foo", "b"))

    // Swift uses string interpolation instead of sprintf
    let s = "sprintf: a \("string")"
    print(s)

    // Swift doesn't have a direct equivalent to Fprintf, but you can use print() with custom streams
    print("io: an error", to: &StandardError())
}

// A custom TextOutputStream for stderr
struct StandardError: TextOutputStream {
    func write(_ string: String) {
        FileHandle.standardError.write(Data(string.utf8))
    }
}

main()

This Swift code demonstrates various string formatting techniques that are analogous to the Go examples. Swift uses string interpolation as its primary method for formatting, which is often more readable than the format string approach used in many other languages.

For more complex formatting needs, Swift provides NumberFormatter and String(format:) which can be used to achieve similar results to Go’s formatting verbs.

To run this program, save it as string-formatting.swift and use the Swift compiler:

$ swift string-formatting.swift

This will compile and run the program, displaying the formatted output.

Swift doesn’t have a direct equivalent to Go’s go build for creating standalone executables. Instead, you typically use Xcode or the Swift Package Manager for building and managing Swift projects.