Reference: Highlight.js Swift tests
Attributes
@convention(swift)
@objc
@objc(name)
@propertyWrapper
@SomeWrapper(value: 1.0, other: "string", bool: false)
@resultBuilder
@ notAnAttribute
Availability
#available()
#available(iOS 15.0, *)
@available()
@available(iOS 15.0, *)
@available(iOS 14, deprecated: "reason", *)
#unavailable()
#unavailable(iOS 15.0, *)
// not a keyword
@unavailable()
@unavailable(iOS 15.0, *)
Functions
func f1<
X,
Y: A,
// documentation
Z: B & C<D>
>() where X == Y, Y: A, Z: B & C<D> { }
func < <T>() { }
func f2(_ p: @escaping () throws -> Void) rethrows -> some Collection { }
func f3(
p1e p1i: inout Int = 5,
_ p2: (x: Int, y: Int),
p3: (var: Int, let: Int) throws -> Int,
p4: Int...
p5: @attribute String? = "text"
) { }
init<X: A>(_ p: @attribute inout (x: Int, var: Int) = (0, 0)) { }
init?(_ p: @attribute inout (x: Int, var: Int) = (0, 0)) { }
init! (_ p: @attribute inout (x: Int, var: Int) = (0, 0)) { }
subscript<X: A>(_ p: @attribute inout (x: Int, var: Int) = (0, 0)) { }
protocol Comparable: Equatable {
static func < (lhs: Self, rhs: Self) -> Bool
static func <= (lhs: Self, rhs: Self) -> Bool
static func > (lhs: Self, rhs: Self) -> Bool
static func >= (lhs: Self, rhs: Self) -> Bool
}
Identifiers
`func`
{ $0 + 1 }
value.$wrappedValue
Keywords
Sequence.Protocol Protocol
String.Type Type
String.init init
String.self self
Any Self
(_ name: String)
x as Int
x as? Double
x as! String
x is String
init? init! init
try? try! try
true false nil
fileprivate(set) internal(set) open(set) private(set) public(set)
unowned(safe) unowned(unsafe)
async await
isolated nonisolated
#if
#error("Error")
#endif
x.as(y)
x.for(y)
#notAKeyword
Numbers
1; 10; 999; 0; 00; 000; 08; 019
1_; 1_0; 9_9__9; 0; 0_0; 0_0_0__; 0__8; 01_9
0b0; 0b11; 0b000; 0b010; 0b01011
0b0_; 0b1_1; 0b0_00; 0b01_0; 0b01__0_1__1_
0o77; 0o0; 0o01; 0o777; 0o644
0o7_7_; 0o0; 0o0_1; 0o77__7; 0o6_44__
0x0; 0xa0; 0x7FFF; 0xd3aD
0x0_; 0xa__0; 0x7F_FF; 0xd3_aD__
4.2; 4.20; 88.0; 13.37
4.2_; 4.2__0; 8_8.0; 13_.3_7__
0e10; 0.00e+10; 9e-10; 4.2E10; 40.0E+10; 0e100; 1010e+10
0e1_0; 0.0__0e+1_0; 9_e-1__0_; 4.2_E1_0; 4_0_.0E+1_0; 0e1_0_0; 10_10e+10_
0x0p0; 0x1.ep6; 0xa0p+01; 0x0.7FFp-18; 0xd3aD.B00p9
0x0_P0_; 0x1__.Ep6; 0xa_0p+0__1; 0x0.7__F_F_P-1__8_; 0xd__3_aD.b00__p9
// expressions containing numeric literals
+0
+-1
2-3
-10.magnitude
fn(-5)
0x2.p2
// expressions not containing numeric literals
fn(x0.d);
// pseudo-expressions containing numeric literals
.0
0.
// invalid pseudo-numeric expressions
_0
0b_0
0b02
0B0
0o_0
0o08
0O7
0x_0
0x0G
0X0
1e_1
0b1e1
0o1e1
0x0pA
0x.1p1
Operator declarations
prefix operator +++
postfix operator +++
infix operator +-: AdditionPrecedence
Operators
!true
~x
+1
-1
..<1
...1
0...
a?
a!
a << 1
a<<1
a >> 1
a>>1
a * 1
a*1
a / 1
a/1
a % 1
a%1
a &* 1
a&*1
a & b
a&b
a + 1
a+1
a - 1
a-1
a &+ 1
a&+1
a &- 1
a&-1
a | b
a|b
a ^ b
a^b
0 ..< 1
0..<1
0 ... 1
0...1
a ?? 1
a??1
a < 1
a<1
a <= 1
a<=1
a > 1
a>1
a >= 1
a>=1
a == 1
a==1
a != 1
a!=1
a === b
a===b
a !== b
a!==b
a ~= 1
a~=1
a .== b
a.==b
a .!= b
a.!=b
a .< b
a.<b
a .<= b
a.<=b
a .> b
a.>b
a .>= b
a.>=b
true && false
true&&false
true || false
true||false
a = 1
a=1
a *= 1
a*=1
a /= 1
a/=1
a %= 1
a%=1
a += 1
a+=1
a -= 1
a-=1
a <<= 1
a<<=1
a >>= 1
a>>=1
a &= b
a&=b
a |= b
a|=b
a ^= b
a^=b
Precedence group
precedencegroup MyGroup {
higherThan: OtherGroup, AnotherGroup
lowerThan: OtherGroup, AnotherGroup
assignment: true
associativity: left
associativity: right
associativity: none
}
Strings
"escaped characters \0\\\t\n\r\"\'"
"escaped unicode \u{0}\u{12}\u{345}\u{6789}\u{abcde}\u{fABCDE}\u{F012345}\u{67890abc}"
#"escaped characters \#0\#\\#t\#n\#r\#"\#'"#
#"escaped unicode \#u{0}\#u{12}\#u{345}\#u{6789}\#u{abcde}\#u{fABCDE}\#u{F012345}\#u{67890abc}"#
#"raw characters \0\\\t\n\r\"\'\u{6789}"#
##"escaped characters \##0\##\\##t\##n\##r\##"\##'"##
##"escaped unicode \##u{0}\##u{12}\##u{345}\##u{6789}\##u{abcde}\##u{fABCDE}\##u{F012345}\##u{67890abc}"##
##"raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789}"##
###"escaped characters \###0\###\\###t\###n\###r\###"\###'"###
###"escaped unicode \###u{0}\###u{12}\###u{345}\###u{6789}\###u{abcde}\###u{fABCDE}\###u{F012345}\###u{67890abc}"###
###"raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789} \##0\##\\##t\##n\##r\##"\##'\##u{6789}"###
"""
escaped characters \0\\\t\n\r\"\'
escaped unicode \u{0}\u{12}\u{345}\u{6789}\u{abcde}\u{fABCDE}\u{F012345}\u{67890abc}
"""
#"""
escaped characters \#0\#\\#t\#n\#r\#"\#'
escaped unicode \#u{0}\#u{12}\#u{345}\#u{6789}\#u{abcde}\#u{fABCDE}\#u{F012345}\#u{67890abc}
raw characters \0\\\t\n\r\"\'\u{6789}
"""#
##"""
escaped characters \##0\##\\##t\##n\##r\##"\##'
escaped unicode \##u{0}\##u{12}\##u{345}\##u{6789}\##u{abcde}\##u{fABCDE}\##u{F012345}\##u{67890abc}
raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789}
"""##
###"""
escaped characters \###0\###\\###t\###n\###r\###"\###'
escaped unicode \###u{0}\###u{12}\###u{345}\###u{6789}\###u{abcde}\###u{fABCDE}\###u{F012345}\###u{67890abc}
raw characters \0\\\t\n\r\"\'\u{6789} \#0\#\\#t\#n\#r\#"\#'\#u{6789} \##0\##\\##t\##n\##r\##"\##'\##u{6789}
"""###
"""
escaped newline \
same line
"""
#"""
escaped newline \#
same line
"""#
##"""
escaped newline \##
same line
"""##
###"""
escaped newline \###
same line
"""###
"interpolation \(x)"
#"interpolation \#(123) raw \(123)"#
##"interpolation \##(1.23) raw \#(1.23) raw \(1.23)"##
###"interpolation \###("string") raw \##("string") raw \#("string") raw \("string")"###
"""
interpolation \($0 + 1)
"""
#"""
interpolation \#(abs(x - 2) as Double)
raw \(abs(x - 2) as Double)
"""#
##"""
interpolation \##(true)
raw \#(true)
raw \(true)
"""##
###"""
interpolation \###("string")
raw \##("string")
raw \#("string")
raw \("string")
"""###
SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
#if os(iOS)
Text("Hello, world from iOS!")
#else
Text("Hello, world!")
#endif
}
}
}
Tuples
(3, "string")
(c: (x: 1, y: 1), z: 1)
(var: Array<Int>, let: Array<Double>)
(_ x: inout Int) throws -> Int
(abs(-2), abs(2))
(x < y, a > b)
($0, $1)
(@escaping (String) -> Void, @autoclosure () -> String) -> String
(
// x
x,
/* y */
y
)
(let x, var y)
([key: value, key: value])
Type definitions
class TestClass {}
struct TestStruct {}
enum TestEnum {}
actor TestActor {}
extension TestExtension {}
protocol TestProtocol {}
Types
// Simple identifier
Int
// Types from Apple frameworks
CALayer
CGRect
MKMapView
NSView
UIView
XCTest
// ?, !, ..., and & should not be highlighted as operators
Int?
Int!
Int?!
String...
SomeClass & SomeProtocol
// Arrays, dictionaries, and nested types
[String]
Array<String>
[[Int?]]
Array<Array<Int?>>
[String: Int]
Dictionary<String, Int>
Swift.Array<Int>.Element
// Tuples
()
(Double, Double)
(x: Double, y: Double)
// Functions
(@escaping (String) -> Void, @autoclosure () -> String) -> String
(Int, String...) -> some Collection
() throws -> Self
// Generic arguments
Array<String.Type>
Array<Sequence.Protocol>
Dictionary<String, Any>
Dictionary<String, Array<Int>>
Array<(@autoclosure () -> String) throws -> String?>
Array<
// documentation
Int
>