Difference between revisions of "Swift Live Reference"

From Coder Merlin
(Added basic page structure.)
 
m (Editorial review and minor corrections)
 
(22 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Comments ==
== Comments ==
Comments are used by humans to document code for other humans. They have no effect on the program itself.
Humans use ''comments'' to document code for other humans. They have no effect on the program's operation itself.
=== Single-line comments ===
=== Single-line comments ===
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=1
|height=100
|language=swift
|initialCode=
// single-line comments begin with two slashes
// single-line comments begin with two slashes
</syntaxhighlight>
}}
 
=== Multi-line comments ===
=== Multi-line comments ===
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=2
|height=100
|language=swift
|initialCode=
/* multi-line comments begin with slash-star
/* multi-line comments begin with slash-star
   and continue  
   and continue  
   and continue
   and continue
   until a closing star-slash */
   until a closing star-slash */
</syntaxhighlight>
}}
 
== Output ==
=== print ===
{{CodeExplorer
|exerciseID=3
|height=100
|language=swift
|initialCode=
print("Hello, World!")
}}
 
=== print with separator ===
{{CodeExplorer
|exerciseID=4
|height=100
|language=swift
|initialCode=
print("to", "ease", "another's", "heartache", separator: "*")
}}
 
=== print with terminator ===
{{CodeExplorer
|exerciseID=5
|height=100
|language=swift
|initialCode=
print("to", "ease", "another's", "heartache", separator: "*", terminator: "<>")
print("is", "to", "forget", "one's", "own", separator: "/")
}}
 
== Types ==
In Swift, it is customary to capitalize all types.
;Int:A signed integer value type
;Float:A single-precision, floating-point value type
;Double:A double-precision, floating-point value type
;String:A Unicode string value that is a collection of characters
 
== Constant and Variable Declarations ==
=== Constant declarations ===
Constants (named values that should never change) use the '''let''' keyword.
{{CodeExplorer
|exerciseID=6
|height=100
|language=swift
|initialCode=
let gravity = 9.81 // meters per second per second
print(gravity)
}}
 
=== Variable Declarations ===
Variables (named values that could change) use the '''var''' keyword.
{{CodeExplorer
|exerciseID=7
|height=100
|language=swift
|initialCode=
var currentTemperature = 23.1 // degrees celsius
print(currentTemperature)
}}
 
== Control Structures ==
== Control Structures ==
Control structures are used to alter the otherwise sequential flow of a program.
Control structures are used to alter the otherwise sequential flow of a program.
=== Conditionals ===
=== Conditionals ===
Conditionals are used to execute a segment of code based upon a condition being met.
Conditionals are used to execute a segment of code if a condition is met.
==== if ====
==== if ====
<syntaxhighlight lang="swift">
{{CodeExplorer
|exerciseID=9
|height=100
|language=swift
|initialCode=
var n = 7
if n < 10 {
if n < 10 {
     print("n is less than 10")
     print("n is less than 10")
}
}
</syntaxhighlight>
}}
 
==== else ====
==== else ====
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
Line 31: Line 106:
</syntaxhighlight>
</syntaxhighlight>
==== switch ====
==== switch ====
<syntaxhighlight lang="swift">
switch n {
    case 0:
        print("n is zero")
    case 1:
        print("n is one")
    default:
        print("n is neither one nor zero")
}
</syntaxhighlight>
=== Loops ===
=== Loops ===
Loops are used to repeat a segment of code until a condition is met.
Loops are used to repeat a segment of code until a condition is met.
==== for ====
==== for ====
The '''for''' loop is helpful when the number of iterations is known.
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
// From zero up to but excluding ten
for n in 0 ..< 10 {
for n in 0 ..< 10 {
    print(n)
}
// From one up to and including 10
for n in 1 ... 10 {
     print(n)
     print(n)
}
}
</syntaxhighlight>
</syntaxhighlight>
==== while ====
==== while ====
The '''while''' loop is helpful when the condition might initially be false and the statements should not be executed.
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
while n < 10 {
while n < 10 {
Line 45: Line 139:
}
}
</syntaxhighlight>
</syntaxhighlight>
==== repeat while ====
==== repeat while ====
The '''repeat while''' loop is helpful when the statements should be executed ''at least'' once.
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
repeat {
repeat {
Line 51: Line 147:
} while n < 10
} while n < 10
</syntaxhighlight>
</syntaxhighlight>
== References ==
* https://developer.apple.com/documentation/swift

Latest revision as of 12:14, 11 February 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Comments[edit]

Humans use comments to document code for other humans. They have no effect on the program's operation itself.

Single-line comments[edit]

CoderMerlin™ Code Explorer: W0000 (1) 🟢


Multi-line comments[edit]

CoderMerlin™ Code Explorer: W0000 (2) 🟢


Output[edit]

print[edit]

CoderMerlin™ Code Explorer: W0000 (3) 🟢


print with separator[edit]

CoderMerlin™ Code Explorer: W0000 (4) 🟢


print with terminator[edit]

CoderMerlin™ Code Explorer: W0000 (5) 🟢


Types[edit]

In Swift, it is customary to capitalize all types.

Int
A signed integer value type
Float
A single-precision, floating-point value type
Double
A double-precision, floating-point value type
String
A Unicode string value that is a collection of characters

Constant and Variable Declarations[edit]

Constant declarations[edit]

Constants (named values that should never change) use the let keyword.

CoderMerlin™ Code Explorer: W0000 (6) 🟢


Variable Declarations[edit]

Variables (named values that could change) use the var keyword.

CoderMerlin™ Code Explorer: W0000 (7) 🟢


Control Structures[edit]

Control structures are used to alter the otherwise sequential flow of a program.

Conditionals[edit]

Conditionals are used to execute a segment of code if a condition is met.

if[edit]

CoderMerlin™ Code Explorer: W0000 (9) 🟢


else[edit]

if n < 10 {
    print("n is less than 10")
} else {
    print("n is not less than 10")
}

switch[edit]

switch n {
    case 0:
        print("n is zero")
    case 1:
        print("n is one")
    default:
        print("n is neither one nor zero")
}

Loops[edit]

Loops are used to repeat a segment of code until a condition is met.

for[edit]

The for loop is helpful when the number of iterations is known.

// From zero up to but excluding ten
for n in 0 ..< 10 {
    print(n)
}

// From one up to and including 10
for n in 1 ... 10 {
    print(n)
}

while[edit]

The while loop is helpful when the condition might initially be false and the statements should not be executed.

while n < 10 {
    n += 1
}

repeat while[edit]

The repeat while loop is helpful when the statements should be executed at least once.

repeat {
    n += 1
} while n < 10

References[edit]