(M2-MAIN) Swift Syntax
(M2-MAIN) Swift Syntax
Development 2
IT0093
Module 2
Swift Syntax
• To familiarize with swift basic syntax
• To use implicitly and explicitly define a data type
• To understand the advance control structure of swift
• To manipulate string
• To define an optional and optional binding
• To use tuples and collection in creating an application
Introduction to Swift
The value of a constant cannot be changed once it is set,
whereas a variable can be set to a different value in the
future
Constants uses the let keyword
Variables uses the var keyword
You can provide a type annotation when you declare a
constant or variable, to be clear about the kind of values
the constant or variable can store.
Int
• On a 32-bit platform, Int is the same size as Int32.
• On a 64-bit platform, Int is the same size as Int64.
Uint
• On a 32-bit platform, UInt is the same size as UInt32.
• On a 64-bit platform, UInt is the same size as UInt64.
Floating-point numbers are numbers with a fractional component, such
as 3.14159, 0.1, and -273.15.
String Literals
A string literal is a fixed sequence of textual characters
surrounded by a pair of double quotes ("").
You indicate whether a particular String can be modified
(or mutated) by assigning it to a variable (in which case
it can be modified), or to a constant (in which case it
cannot be modified):
Swift’s String type is a value type. If you create a new String
value, that String value is copied when it is passed to a
function or method, or when it is assigned to a constant or
variable.
Empty Array
Array with a Default Value
Or
inferred
A set stores distinct values of the same type in a collection with no
defined ordering. You can use a set instead of an array when the order
of items is not important, or when you need to ensure that an item only
appears once.
Inferred
• Use the intersect(_:) method to create a new set with only the values common to
both sets.
• Use the exclusiveOr(_:) method to create a new set with values in either set, but
not both.
• Use the union(_:) method to create a new set with all of the values in both sets.
• Use the subtract(_:) method to create a new set with values not in the specified
set.
• Use the “is equal” operator (==) to determine whether two sets contain all of the same
values.
• Use the isSubsetOf(_:) method to determine whether all of the values of a set are contained
in the specified set.
• Use the isSupersetOf(_:) method to determine whether a set contains all of the values in a
specified set.
• Use the isStrictSubsetOf(_:) or isStrictSupersetOf(_:) methods to determine whether a set is
a subset or superset, but not equal to, a specified set.
• Use the isDisjointWith(_:) method to determine whether two sets have any values in
common.
A dictionary stores associations between keys of the same type and
values of the same type in a collection with no defined ordering. Each
value is associated with a unique key, which acts as an identifier for
that value within the dictionary.
Empty Dictionary
Dictionary Literals
inferred