Reference: Prism.js Swift tests
Attributes
@IBOutlet
@IBDesignable
@IBAction
@IBInspectable
@class_protocol
@exported
@globalActor
@MainActor
@noreturn
@NSCopying
@NSManaged
@objc
@propertyWrapper
@UIApplicationMain
@auto_closure
@SomeCustomName
Class names
struct SomeStructure {}
enum SomeEnumeration {}
class SomeClass: SomeSuperclass {
class var overrideableComputedTypeProperty: Int {
return 107
}
}
Comments
// foo
/**/
/* foo */
/*
foo
*/
/*
/*
foo
*/
*/
Constants (not supported)
nil
AB
FOO_BAR
kAb
kFoo_bar
Directives
#if os(tvOS)
#if !DEBUG && ENABLE_INTERNAL_TOOLS
#if SWIFTUI_PROFILE
#if compiler(>=5)
#if compiler(>=5) && swift(<5)
#elseif compiler(>=5)
#else
#endif
#sourceLocation(file: "foo", line: 42)
#sourceLocation()
#error("error message")
#warning("warning message")
#available(iOS 13, *)
#selector(SomeClass.doSomething(_:))
#keyPath(SomeClass.someProperty)
Functions
func greetAgain(person: String) -> String {
return "Hello again, " + person + "!"
}
print(greetAgain(person: "Anna"))
func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
// none of the below are functions
subscript(index: Int) -> Int {
get {}
set(newValue) {}
}
Keywords
Any
Protocol
Self
Type
actor
as
assignment
associatedtype
associativity
async
await
break;
case
catch
class;
continue;
convenience
default
defer
deinit
didSet
do
dynamic
else
enum
extension
fallthrough
fileprivate
final
for
func;
get
guard
higherThan
if
import
in
indirect
infix
init
inout
internal
is
isolated
lazy
left
let
lowerThan
mutating
none
nonisolated
nonmutating
open
operator
optional
override
postfix
precedencegroup
prefix
private
protocol
public
repeat
required
rethrows
return
right
safe
self
set
some
static
struct
subscript
super
switch
throw
throws
try
typealias
unowned
unsafe
var
weak
where
while
willSet
Labels
gameLoop: while square != finalSquare {
break gameLoop
continue gameLoop
}
Literals
#file
#fileID
#filePath
#line
#column
#function
#dsohandle
#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
#fileLiteral(resourceName: "foo")
#imageLiteral(resourceName: "foo")
Numbers
// Checks for decimal, hexadecimal, octal and binary numbers.
42
42_000
3.1415_9
4.2e14
0xBaf_Face
0xFF47.AB_61p2
0b0000_1111
0o147_654
Operators (not supported)
+ - * / %
+= -= *= /= %=
~ & | ^ << >>
~= &= |= ^= <<= >>=
&+ &- &* &<< &>>
&+= &-= &*= &<<= &>>=
=
== != === !== <= >= < >
! && ||
..< ...
->
??
// custom operators
+++
prefix func +++ (vector: inout Vector2D) -> Vector2D {}
// dot operators (SIMD)
.!= .== .< .> .<= .>=
Punctuations
{ } [ ] ( )
; , . :
\
Short arguments
reversedNames = names.sorted(by: { $0 > $1 } )
Strings
""
"fo\"o"
"foo\
bar"
"foo /* not a comment */ bar"
"foo\
/* not a comment */\
bar"
let softWrappedQuotation = """
The White Rabbit put on his spectacles. "Where shall I begin, \
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on \
till you come to the end; then stop."
"""
let threeMoreDoubleQuotationMarks = #"""
Here are three more double quotes: """
"""#
#"Write an interpolated string in Swift using \(multiplier)."#
"foo \(42)"
"foo \(f("bar"))"
"\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
#"6 times 7 is \#(6 * 7)."#