Home » Swift Basic Syntax

Swift Basic Syntax

by Online Tutorials Library

Swift Syntax

Comments in Swift

Comments are used in programs to make them clearly understandable. They are like helping texts in programs and ignored by compiler. In Swift 4, single line comments are written using // at the beginning of the comment.

Single line comment in Swift 4:

Multi-line comment is Swift 4:

Multi-line comments start with /* and end with the characters */ as shown below –

Multi-line comments can be nested in Swift 4. i.e.


Semicolons in Swift

In Swift 4, you don’t need to type a semicolon (;) in your code as a closing statement. Though, it is optional, you can use it without any problem. If you are using multiple statements in the same line, then you have to use a semicolon as a delimiter, otherwise the compiler will raise a syntax error.

For example,

Without using semicolon:


Identifiers in Swift

In Swift 4, identifiers are used to identify a variable, function, or any other user-defined item. Swift 4 identifiers start with an alphabet A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).

In Swift 4, we can’t use special characters such as @, $, and % within identifiers. Swift 4 is a case sensitive programming language, so Literal and literal are two different identifiers.

These are some example of acceptable identifiers:

If you want to use a reserved word as an identifier you will have to put a backtick (`) before and after that reserved word. For example, class is not a valid identifier, but `class` is valid.


Reserved Keywords in Swift

In Swift 4, the reserved keywords can’t be used as constants or variables or any other identifier names. If you want to use them as identifiers, you will use them within backticks (‘).

Keywords used in declarations

Class Func Let public
deinit Enum extension import
Init internal operator private
protocol static struct subscript
typealias var

Keywords used in Statements

break case continue default
do else fallthrough for
if in return switch
where while

Keywords used in expressions and types

as dynamicType false is
nil self Self super
true _COLUMN_ _FILE_ _FUNCTION_
_LINE_

Keywords used in particular contexts

associativity convenience dynamic didSet
final get infix inout
lazy left mutating none
nonmutating optional override postfix
precedence prefix Protocol required
right set Type unowned
weak willSet

Whitespaces in Swift

In Swift 4, whitespace is used to describe blanks, tabs, newline characters, and comments. It separates one part of the statement from another. It makes the computer to identify that here one element ends and another begins.

For example

We have to put at least one whitespace character (usually a space) between var and age to make compiler to distinguish them.

On the other hand, in the following statement –

No whitespace character is necessary between courses and =, or between = and html, although you can include them for better readability.

You should make the equal space on both side of the operator.

For example

The Swift 4 compiler ignores blank line which contains only whitespace.


Literals in Swift

A literal is used to represent the source code of a value of an integer, floating-point number, or string type.

For example

Integer literal

Floating-point literal

String literal


Print Statement in Swift

In Swift4, the ‘print’ keyword is used to print anything. There are three different properties of print keyword.

  1. Items: Items that you want to print.
  2. Separator: Used to separate items.
  3. Terminator: It specifies the last value where the line ends.

For example

The first print statement adds n , newline Feed as terminator by default, where as in second print statement we’ve given ” End ” as terminator, hence it’ll print “End ” instead of n.

We can use custom separator and terminators according to our requirement.


Next TopicSwift Datatypes

You may also like