14 January 2024
Swift Syntax Sample
Sample Swift syntax highlighting using SwiftSnip.
- //
- // CodeSample.swift
- // App
- //
- // Created by Steven Grosmark on 11/24/19.
- //
- import Foundation
- /*
- | multi-line
- | comment, with empty lines
- | see: https://g-mark.com
- */
- open class Blah: NSObject {
-
- enum EmbedError: Error {
- case invalidUrl
- }
-
- override init() {
- super
.init() - let zap = "a"
- var zoob = 213.456 + 3
- zoob += 4
- print(zap); print(zoob)
- }
-
- private var x: Int?
- internal let y: Double = 123.456
- lazy public var rawString = ##"\\Hello \##(y) \\World"##
-
- /// Handle the embed request.
- /// - Parameter req: the request with the url to embed
- @discardableResult
- final func embed(_ req: Request, completion: @escaping (Int) -> Void) throws -> /* Int */ String {
- guard let uri = req
.query[String .self, at: "url"], !uri .isEmpty, - let url = URL(string: uri),
- let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
- (components
.scheme == "http" || components .scheme == "https") else { - return "invalid url"
- }
- #if swift(>=4.2)
- // swift 4.2 only code here
- #elseif swift(>=3.2)
- #warning("message")
- #error("message")
- #endif
-
- return "embed: \(url)"
- }
-
- @MainActor
- func foo(these numbers: Int...) { }
-
- struct Donut {
- let regex1 = /([^0-9]+)/
- let regex2 = #/([^0-9]+)/#
- var a: String
- mutating func boo() { a = "a" }
- }
-
- // MARK: - this is the end
- }
- actor Fresh {
- nonisolated func fresh(
- file: StaticString = #file,
- function: StaticString = #function,
- line: UInt = #line
- ) {
- }
-
- func foo() async throws {
- try await foo()
- Task { @MainActor in
- try await Task
.sleep(for: .seconds(1)) - }
- }
- }
- private let sampleCode =
- """
- func test() {
- print("This is a test.")
- }
- """
- private let anotherSample = """
- func test() {
- print("This is a \((0..<3)
.map(String .init) .joined(separator: ".")).") - // empty line above - but this one is extra long so that I can test word-wrapping in a multiline string.
- // Make \
- one \
- line
- }
- """
- @propertyWrapper
- struct Bloated<T> {
- var wrappedValue: T
- let projectedValue: T
- init(wrappedValue: T) {
- self
.wrappedValue = wrappedValue - self
.projectedValue = wrappedValue - }
- }
- struct Bling {
- @Bloated var zed = 42
- func exposed() {
- _ = $zed
- }
- func `func`() {
- }
- }