Swift Tutorial - Swift Introduction1-Merged
Swift Tutorial - Swift Introduction1-Merged
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Introduction
Swift Introduction ()
Next » (0010__Swift_Constants_Variables.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm) Swift is an object-oriented language.
Swift String
Swift builds upon familiar concepts from Objective-C but includes a modern, safer syntax and
(0040__Swift_String.htm)
Swift Booleans
multiple paradigms such as object-oriented, functional, imperative, and block structured.
(0050__Swift_Booleans.htm)
Swift is a programming language customized by Apple and introduced as Objective-C without the
Swift Tuples
C.
(0060__Swift_Tuples.htm)
Swift Optionals Swift can work with the existing Cocoa and Cocoa Touch frameworks, which contain all the
(0070__Swift_Optionals.htm)
familiar classes used in modern Mac and iOS apps.
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
Swift provides a lot of safety in terms of
type checking,
Operator
constants for immutability,
Swift Assertions
requiring values to be initialized before use,
(0090__Swift_Assertions.htm)
built-in overflow handling, and
Swift Assignment Operators
automatic memory management.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators Swift was built using the highly optimized LLVM compiler.
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) Next » (0010__Swift_Constants_Variables.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/index.htm 1/2
13/02/2025, 11:53 Swift Tutorial - Swift Introduction
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/index.htm 2/2
13/02/2025, 11:54 Swift Tutorial - Swift Constants/Variables
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Constants/Variables
Swift Introduction (index.htm)
« Previous (index.htm) Next » (0020__Swift_Data_Types.htm)
Swift Constants/Variables ()
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
(0050__Swift_Booleans.htm)
Swift Tuples
(0060__Swift_Tuples.htm) Constants
Swift Optionals
(0070__Swift_Optionals.htm) The following code creates a constant and stores the string "Hello World" in a constant named
Swift Global/Local Variables s1 .
(0080__Swift_Global_Local_Variables.htm)
let s1:String = "Hello World"
Operator
Swift Assertions The first part of the constant declaration is the let keyword.
(0090__Swift_Assertions.htm)
The let keyword is used to define a constant.
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm)The next part is the constant name s1 .
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)You also have the type declaration :String .
Swift Comparison Operators
Variables
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm) Variables use the var keyword instead of the let keyword to declare.
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) Variable values can change.
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0010__Swift_Constants_Variables.htm 1/2
13/02/2025, 11:54 Swift Tutorial - Swift Constants/Variables
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0010__Swift_Constants_Variables.htm 2/2
13/02/2025, 11:54 Swift Tutorial - Swift Data Types
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Data
Introduction
Types
Swift Introduction (index.htm)
« Previous (0010__Swift_Constants_Variables.htm) Next » (0030__Swift_Number.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types ()
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
(0050__Swift_Booleans.htm)
The following table describes the common Swift data types.
Swift Tuples
(0060__Swift_Tuples.htm)
Data Type Description
Swift Optionals
(0070__Swift_Optionals.htm)
String Sequence of characters
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
Int Whole number
Swift Enumerations
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm)
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Type Inference
Statement
Swift Comments
Swift compiler can figure data type out based on what value you assign to the variable or
(0190__Swift_Comments.htm) constant. This is called type inference.
Swift for loop
This means you could have omitted the :String from your declarations and instead use
(0200__Swift_for.htm)
Swift while loop something as follows.
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm) var s3 = "java2s.com"
Swift switch
(0230__Swift_switch.htm)
Swift continue break Printing Variables and Constants
(0240__Swift_continue-break.htm)
The following code uses the println function to output string to console.
Function import Foundation
Swift Functions println("Hello, World!")
(0300__Swift_Functions.htm)
Swift Closures
The following code shows how to print Variables.
(0310__Swift_Closures.htm)
import Foundation
Class/Structure let s:String = "Hey There"
Swift Structures println(s)
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0020__Swift_Data_Types.htm 1/2
13/02/2025, 11:54 Swift Tutorial - Swift Data Types
Swift Class println() prints something out and then goes to the next line, print() will print out a value
(0330__Swift_Class.htm) without moving to the next line in the console window.
Swift Properties
(0340__Swift_Properties.htm) import Foundation
Swift Methods print("Print this")
(0350__Swift_Methods.htm) print(" and ")
Swift Subscripts print("that")
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types Swift Type Aliases
(0400__Swift_Nested_Types.htm)
We can add an alternative name for existing types using type aliases.
Swift Extensions
(0410__Swift_Extensions.htm) The following code uses a type alias to define Note as an alternative to String .
Swift Protocols
(0420__Swift_Protocols.htm) typealias Note = String
Swift Generics var n1:Note = "hi"
(0430__Swift_Generics.htm)
You can treat n1 as a string.
www.java2s.com/Tutorials/Swift/Swift_Language/0020__Swift_Data_Types.htm 2/2
13/02/2025, 11:54 Swift Tutorial - Swift Number
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Number
Introduction « Previous (0020__Swift_Data_Types.htm) Next » (0040__Swift_String.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number ()
Swift String
(0040__Swift_String.htm)
Swift Numbers have their own data types.
Swift Booleans
(0050__Swift_Booleans.htm) In Swift some number data types can store larger numbers than others.
Swift Tuples
(0060__Swift_Tuples.htm) Numbers that have a fractional component are treated differently than numbers that are whole
Swift Optionals numbers.
(0070__Swift_Optionals.htm)
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)Integers
Integers are whole numbers that may be either positive or negative.
Operator
Swift Assertions Integers don't have any decimal places.
(0090__Swift_Assertions.htm)
Swift Assignment Operators
For example, 1, 2, and -9 are all integers.
(0100__Swift_Assignment_Operators.htm) Int is used when you don't need to specify a size for the integer.
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)The following code declares Integers
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)let i1:Int = 5
Swift Range Operators var i2 = 7
(0130__Swift_Range_Operators.htm)
Swift Logical Operators i1 is an integer constant, while i2 is an integer variable.
(0140__Swift_Logical_Operators.htm)
Swift has the following int type for various size and unsigned integers: Int8 Int16 Int32 Int64
Statement UInt8 UInt16 UInt32, and UInt64.
Swift Comments
(0190__Swift_Comments.htm) In these data types, the numbers indicate the size of the integer.
Swift for loop
(0200__Swift_for.htm)
For example, Int8 means 8-bit, which gives you an integer range of -127 to 127.
Swift while loop The data types that have a U as the first character are unsigned integers. These integers must
(0210__Swift_while.htm)
be positive. UInt8 gives you a range of 0 to 255.
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
Floating-Point Numbers
(0300__Swift_Functions.htm) Floating-point numbers can have decimal places.
Swift Closures
(0310__Swift_Closures.htm) An example of a floating-point number is 9.99.
To specify a floating-point number, you can use the Float data type, as shown in the following
Class/Structure example.
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0030__Swift_Number.htm 1/2
13/02/2025, 11:54 Swift Tutorial - Swift Number
Swift Class
let f1:Float = 9.99
(0330__Swift_Class.htm) let f2 = 3.14
Swift Properties
(0340__Swift_Properties.htm)
If you leave out the data type and include a number with decimal places, Swift will use type
Swift Methods
(0350__Swift_Methods.htm) inference to figure out the data type as Float data type.
Swift Subscripts
Float is a 32-bit floating-point number, and you should use that when you don't require 64-bit
(0360__Swift_Subscript.htm)
Swift Inheritance
precision.
(0370__Swift_Inheritance.htm) When you do require 64-bit precision, use the Double data type, as shown in the following code.
Swift Initialization
(0380__Swift_Initialization.htm)
let d1:Double = 1.2345
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types « Previous (0020__Swift_Data_Types.htm) Next » (0040__Swift_String.htm)
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0030__Swift_Number.htm 2/2
13/02/2025, 11:55 Swift Tutorial - Swift String
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift String
Introduction « Previous (0030__Swift_Number.htm) Next » (0050__Swift_Booleans.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String ()
A string is a sequence of characters, such as "Hello World!".
Swift Booleans
(0050__Swift_Booleans.htm) Strings use the String data type, although you don't need to specify the data type to use a
Swift Tuples
string.
(0060__Swift_Tuples.htm)
Swift Optionals
var s1 = "Hello World!"
(0070__Swift_Optionals.htm)
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
Unicode Characters
Operator Strings can include any Unicode characters.
Swift Assertions
(0090__Swift_Assertions.htm)
To write single-byte Unicode characters, you must include \x before the two hexadecimal digits.
Swift Assignment Operators Two-byte Unicode characters are prefixed with \u before four hexadecimal digits, and four-byte
(0100__Swift_Assignment_Operators.htm)
Unicode characters have \U written before eight hexadecimal digits.
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)The following code shows an example of using Unicode characters.
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)let percentage = "\x{25}"
Swift Range Operators let snowflake = "\u{2744}"
(0130__Swift_Range_Operators.htm) let heart = "\u{0001F497}"
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0040__Swift_String.htm 1/2
13/02/2025, 11:55 Swift Tutorial - Swift String
Swift Class
//Declare characters and strings
(0330__Swift_Class.htm) let c1:Character = "A"
Swift Properties let c2:Character = "B"
(0340__Swift_Properties.htm) let c3:Character = "C"
Swift Methods let c4:Character = "D"
(0350__Swift_Methods.htm) let c5:Character = "E"
Swift Subscripts let s2 = "FGHIJ"
(0360__Swift_Subscript.htm)
Swift Inheritance //Declare an empty string
(0370__Swift_Inheritance.htm) var alphabet = String()
Swift Initialization
//Concatenate strings, characters and literals
(0380__Swift_Initialization.htm)
alphabet = c1 + c2 + c3 + c4 + c5 + s2 + "KLMNOP"
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm) Comparing Strings
Swift Generics
(0430__Swift_Generics.htm) We can compare two strings with comparison operator == . It returns a boolean (true or false)
value back.
alphabet == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
String Interpolation
To print out different variables, constants, literals, and expressions in a string, use string
interpolation.
www.java2s.com/Tutorials/Swift/Swift_Language/0040__Swift_String.htm 2/2
13/02/2025, 11:55 Swift Tutorial - Swift Booleans
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Booleans
Introduction « Previous (0040__Swift_String.htm) Next » (0060__Swift_Tuples.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
Boolean values can be either true or false.
(0040__Swift_String.htm)
Swift Booleans () Booleans are used to help evaluate logical expressions.
Swift Tuples
(0060__Swift_Tuples.htm) To declare a boolean value, you can assign a value immediately, or you can use the Bool data
Swift Optionals type to declare a boolean variable.
(0070__Swift_Optionals.htm)
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)Example
var b1:Bool
Operator
let b2 = false
Swift Assertions
(0090__Swift_Assertions.htm) let alphabet = "ABCDEFGHIJKLMNOP"
Swift Assignment Operators let b4 = alphabet == "ABCDEFGHIJKLMNOP"
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators The value of b4 is true because the two strings are the same.
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) « Previous (0040__Swift_String.htm) Next » (0060__Swift_Tuples.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0050__Swift_Booleans.htm 1/2
13/02/2025, 11:55 Swift Tutorial - Swift Booleans
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0050__Swift_Booleans.htm 2/2
13/02/2025, 11:55 Swift Tutorial - Swift Tuples
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Tuples
Introduction « Previous (0050__Swift_Booleans.htm) Next » (0070__Swift_Optionals.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
Tuples are ordered lists of values.
(0040__Swift_String.htm)
Swift Booleans In Swift, you can group related elements together as a tuple, reducing the need to depend on
(0050__Swift_Booleans.htm)
complex types, objects, and immutable arrays.
Swift Tuples ()
Swift Optionals
(0070__Swift_Optionals.htm) Example
Swift Global/Local Variables
The following code shows how you would declare a tuple for a rectangle.
(0080__Swift_Global_Local_Variables.htm)
Statement Each name will be filled with the corresponding value from the tuple.
Swift Comments You can now use these variables as usual.
(0190__Swift_Comments.htm)
Swift for loop To ignore some values in a tuple, replace a variable name with an underscore _ .
(0200__Swift_for.htm)
Swift while loop So, if you needed only the x origin for the rectangle, you could do as follows.
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm) var (e, _, _, _) = rectangle2
Swift switch print(e)
(0230__Swift_switch.htm)
Swift continue break You can assign tuple values to either constants or variables.
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
« Previous (0050__Swift_Booleans.htm) Next » (0070__Swift_Optionals.htm)
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0060__Swift_Tuples.htm 1/2
13/02/2025, 11:55 Swift Tutorial - Swift Tuples
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0060__Swift_Tuples.htm 2/2
13/02/2025, 11:55 Swift Tutorial - Swift Optionals
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Optionals
Introduction « Previous (0060__Swift_Tuples.htm) Next » (0080__Swift_Global_Local_Variables.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
If you can't be sure a variable has a value present, you can use optionals.
(0040__Swift_String.htm)
Swift Booleans An optional is a variable that can have either an assigned value or no value at all.
(0050__Swift_Booleans.htm)
Swift Tuples Using optionals is an alternative to setting objects to nil .
(0060__Swift_Tuples.htm)
Swift Optionals () Optionals can be used with any type in Swift including numbers, strings, and objects.
Swift Global/Local Variables You declare optionals like variables or constants, but you must include a ? after the type
(0080__Swift_Global_Local_Variables.htm)
declaration.
Operator Like variables and constants, the data type of an optional may be inferred.
Swift Assertions
(0090__Swift_Assertions.htm)
The following code shows how to declare String Optional.
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm)var s:String?
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)The variable s in the code above is declared as an optional, and the starting value is nil .
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Forced Unwrapping
Advanced Data Type If you are sure that an optional has a value, then you can use an exclamation point ! ! to unwrap
Swift Enumerations
the value.
(0150__Swift_Enumerations.htm)
Swift Arrays For instance, if you know that s has a value, then you could unwrap s and use the value as
(0160__Swift_Arrays.htm)
follows.
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
var s:String?
s = "ABC"
Statement println(s!)
Swift Comments
(0190__Swift_Comments.htm) Attempt to unwrap an optional that has no value, you will get a runtime error.
Swift for loop
(0200__Swift_for.htm) We need to test your optional with an if statement before attempting to unwrap the optional.
Swift while loop
(0210__Swift_while.htm)
The following code shows how to test an optional Before Unwrapping Optionals.
Swift if (0220__Swift_if.htm)
if s{
Swift switch
"The value of s is "
(0230__Swift_switch.htm)
s!
Swift continue break
}else{
(0240__Swift_continue-break.htm) "No value in s"
}
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
Optional Bindings
(0320__Swift_Structures.htm) Optional bindings tests an optional and unwraps the optional in an if statement.
www.java2s.com/Tutorials/Swift/Swift_Language/0070__Swift_Optionals.htm 2/2
13/02/2025, 11:55 Swift Tutorial - Swift Global and Local Variables
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Global
Introduction
and Local Variables
Swift Introduction (index.htm)
« Previous (0070__Swift_Optionals.htm) Next » (0090__Swift_Assertions.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
Scope refers to a portion of a program.
(0050__Swift_Booleans.htm)
Swift Tuples We can access variables only from the portion of the program where the variable has been
(0060__Swift_Tuples.htm)
declared.
Swift Optionals
(0070__Swift_Optionals.htm) Global scope variables can be accessed from anywhere in your Swift program.
Swift Global/Local Variables ()
Portions of the program are defined by various Swift constructs such as if statements,
Operator functions, and classes.
Swift Assertions
You will see curly brackets {} used with Swift constructs to define a scope for part of your
(0090__Swift_Assertions.htm)
Swift Assignment Operators
program.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)
Example
Swift Comparison Operators The following code uses if statement as an example to demonstrate scope.
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators let isTrue = true
(0130__Swift_Range_Operators.htm)
Swift Logical Operators if(isTrue){
(0140__Swift_Logical_Operators.htm) var myString = "This is a true statement"
println(myString)
}
Advanced Data Type
Swift Enumerations
(0150__Swift_Enumerations.htm)
The code above declares a boolean constant called isTrue and uses an if statement to test to
Swift Arrays
(0160__Swift_Arrays.htm)
see whether isTrue is set to a true value.
Swift Dictionaries We define a scope with curly brackets {} and put the code in between the curly brackets.
(0170__Swift_Dictionaries.htm)
myString is a variable that is local to the scoped area of if statement. We can access myString
Statement only in that scoped area.
Swift Comments
(0190__Swift_Comments.htm)
Since isTrue is a global variable, you can use that anywhere in your program, including the
Swift for loop scoped area after if statement.
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break « Previous (0070__Swift_Optionals.htm) Next » (0090__Swift_Assertions.htm)
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0080__Swift_Global_Local_Variables.htm 1/2
13/02/2025, 11:55 Swift Tutorial - Swift Global and Local Variables
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0080__Swift_Global_Local_Variables.htm 2/2
13/02/2025, 11:56 Swift Tutorial - Swift Assertions
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Assertions
Introduction « Previous (0080__Swift_Global_Local_Variables.htm) Next » (0100__Swift_Assignment_Operators.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
Assertions can help with your debugging efforts.
(0040__Swift_String.htm)
Swift Booleans Assertions work by testing a condition to see whether the condition is satisfied.
(0050__Swift_Booleans.htm)
Swift Tuples If the condition is false, then a runtime error occurs, and the program stops executing.
(0060__Swift_Tuples.htm)
Swift Optionals Assertions may have an optional message that is printed in the console window when the
(0070__Swift_Optionals.htm) condition is false.
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
Example
Operator To create an assertion, write the assertion shown as follows.
Swift Assertions ()
Swift Assignment Operators var myValue = 4
(0100__Swift_Assignment_Operators.htm)assert(myValue == 3, " myValue must be three ")
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)The assert statement in the code above is testing to make sure that the myValue variable has
Swift Comparison Operators the correct number.
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators Assertions are tested only in Debug mode.
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0090__Swift_Assertions.htm 1/2
13/02/2025, 11:56 Swift Tutorial - Swift Assertions
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0090__Swift_Assertions.htm 2/2
13/02/2025, 11:56 Swift Tutorial - Swift Assignment Operators
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Assignment Operators
Swift Introduction (index.htm)
« Previous (0090__Swift_Assertions.htm) Next » (0110__Swift_Arithmetic_Operators.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
To initialize or update a value, use the assignment operator = .
(0050__Swift_Booleans.htm)
Swift Tuples
var a = 1
(0060__Swift_Tuples.htm)
let b = 2 //Assigning a constant value
Swift Optionals
(0070__Swift_Optionals.htm) a = 3 //Updating Values
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
let (c, d) = (4, 5) // Multiple Assignments
Advanced Data Type The following code shows how to extract a Tuple Value
Swift Enumerations
let (_,_,f) = e
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm) In the code above f has the same value as the third value in the tuple e.
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop Compound Operators
(0200__Swift_for.htm)
The assignment operator is often combined with an arithmetic operator to perform an arithmetic
Swift while loop
operator and assign the result to the value.
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm) The following code shows how to use Compound Assignment/Arithmetic Operations.
Swift switch
(0230__Swift_switch.htm) //Declare and assign i integer equal to 0
Swift continue break var i = 0
(0240__Swift_continue-break.htm)
//Add 4 to i and assign result back to i
Function i+=4
Swift Functions
//Multiple i by 2 and assign result back to i
(0300__Swift_Functions.htm)
i*=2
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0100__Swift_Assignment_Operators.htm 1/2
13/02/2025, 11:56 Swift Tutorial - Swift Assignment Operators
Swift Class « Previous (0090__Swift_Assertions.htm) Next » (0110__Swift_Arithmetic_Operators.htm)
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0100__Swift_Assignment_Operators.htm 2/2
13/02/2025, 11:56 Swift Tutorial - Swift Arithmetic Operators
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Arithmetic
Introduction
Operators
Swift Introduction (index.htm)
« Previous (0100__Swift_Assignment_Operators.htm) Next » (0120__Swift_Comparison_Operators.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
You use the arithmetic operators to do addition + , subtraction - , multiplication * , and division
(0050__Swift_Booleans.htm)
Swift Tuples /.
(0060__Swift_Tuples.htm)
The following code shows how to use Arithmetic Operators.
Swift Optionals
(0070__Swift_Optionals.htm)
let a1 = 1 + 2
Swift Global/Local Variables
let a2 = 3 - 1
(0080__Swift_Global_Local_Variables.htm)
let a3 = 6 * 5
let a4 = 12 / 3
Operator
Swift Assertions All of the examples in the code above result in integer output.
(0090__Swift_Assertions.htm)
Swift Assignment Operators If one of the values is a Float or Double data type, then the result will be a floating-point data
type as well.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators ()
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) Remainder Operator
Advanced Data Type To get the remainder part of the result, you can use the remainder operator % .
Swift Enumerations The following code shows how to use Division and Remainder Operators
(0150__Swift_Enumerations.htm)
Swift Arrays let r5 = 13 / 3
(0160__Swift_Arrays.htm) let r6 = 13 % 3
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Function In the code above we increment with the ++ as a suffix, you still get the original value reported.
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0110__Swift_Arithmetic_Operators.htm 1/2
13/02/2025, 11:56 Swift Tutorial - Swift Arithmetic Operators
Swift Class
(0330__Swift_Class.htm)
Compound Operators
Swift Properties Arithmetic operators can be combined with the assignment operator to perform an arithmetic
(0340__Swift_Properties.htm)
function and assign the result to the value.
Swift Methods
(0350__Swift_Methods.htm)
var i = 0 //Declare and assign i integer equal to 0
Swift Subscripts
i+=4 //Add 4 to i and assign result back to i
(0360__Swift_Subscript.htm)
i*=2 //Multiple i by 2 and assign result back to i
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
String Concatenation
Swift Type Casting The addition operator + is also used to add strings and characters together.
(0390__Swift_Type_Casting.htm)
Swift Nested Types let s1 = "Hello"
(0400__Swift_Nested_Types.htm) let s2 = "World"
Swift Extensions let s3 = s1 + " " + s2 + "!"
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm) « Previous (0100__Swift_Assignment_Operators.htm) Next » (0120__Swift_Comparison_Operators.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0110__Swift_Arithmetic_Operators.htm 2/2
13/02/2025, 11:57 Swift Tutorial - Swift Comparison Operators
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Comparison Operators
Swift Introduction (index.htm)
« Previous (0110__Swift_Arithmetic_Operators.htm) Next » (0130__Swift_Range_Operators.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
We use comparison operators to compare two values. We can test for equality or whether one
(0050__Swift_Booleans.htm)
Swift Tuples value is greater or less than another value.
(0060__Swift_Tuples.htm)
The following code describes the comparison operators available in Swift.
Swift Optionals
(0070__Swift_Optionals.htm)
Operator Description
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
x == y Equal to
(0230__Swift_switch.htm)
//Returns false
Swift continue break
let b2 = x == y
(0240__Swift_continue-break.htm)
Comparison operators are often used with if statements to control program flow.
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0120__Swift_Comparison_Operators.htm 1/2
13/02/2025, 11:57 Swift Tutorial - Swift Comparison Operators
Swift Class The ternary conditional operator is written like this: condition ? action1 : action2 .
(0330__Swift_Class.htm)
Swift Properties The condition is an expression that returns a boolean true or false .
(0340__Swift_Properties.htm)
If the condition returns true , then the first action takes place. If the condition returns false ,
Swift Methods
(0350__Swift_Methods.htm) then the second action takes place.
Swift Subscripts
The following code shows how to use Ternary Conditional Operator.
(0360__Swift_Subscript.htm)
Swift Inheritance
let a = 5
(0370__Swift_Inheritance.htm)
a == 5 ? "We're good" : "Oops, not quite"
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting « Previous (0110__Swift_Arithmetic_Operators.htm) Next » (0130__Swift_Range_Operators.htm)
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0120__Swift_Comparison_Operators.htm 2/2
13/02/2025, 11:57 Swift Tutorial - Swift Range Operators
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Range
Introduction
Operators
Swift Introduction (index.htm)
« Previous (0120__Swift_Comparison_Operators.htm) Next » (0140__Swift_Logical_Operators.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
You use range operators to specify a range of integers such as 1 through 10.
(0050__Swift_Booleans.htm)
Swift Tuples There are two types of range operators: the closed range operator ... and the half-open range
(0060__Swift_Tuples.htm)
operator ..< .
Swift Optionals
(0070__Swift_Optionals.htm)
Swift Global/Local Variables Closed Range Operator
(0080__Swift_Global_Local_Variables.htm)
The closed range operator specifies a range of numbers including the number that defines the
Operator end of the range.
Swift Assertions You must specify the beginning of the range and the end of the range with the closed range
(0090__Swift_Assertions.htm)
operator ... .
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm)
The code in the following code produces output that includes i = 1 through i = 10.
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm) for i in 1...10 {
Swift Comparison Operators println("i = \(i)")
(0120__Swift_Comparison_Operators.htm)}
Swift Range Operators ()
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
for i in 0..<10 {
Statement
println("i = \(i)")
Swift Comments
}
(0190__Swift_Comments.htm)
Swift for loop
The code above generates the following result.
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0130__Swift_Range_Operators.htm 1/2
13/02/2025, 11:57 Swift Tutorial - Swift Range Operators
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0130__Swift_Range_Operators.htm 2/2
13/02/2025, 11:57 Swift Tutorial - Swift Logical Operators
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Logical
Introduction
Operators
Swift Introduction (index.htm)
« Previous (0130__Swift_Range_Operators.htm) Next » (0150__Swift_Enumerations.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
The logical operators are used with boolean values and expressions that return boolean values.
(0050__Swift_Booleans.htm)
Swift Tuples Logical operators are used to test whether two expressions are true or whether one expression
(0060__Swift_Tuples.htm)
is true.
Swift Optionals
(0070__Swift_Optionals.htm) The following table describes the logical operators supported by Swift.
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)Operator Description
Swift Enumerations
(0150__Swift_Enumerations.htm) The following code shows how to use Logical OR.
Swift Arrays
(0160__Swift_Arrays.htm) let x = true
Swift Dictionaries let y = false
(0170__Swift_Dictionaries.htm)
let b = x || y
Statement
The following code shows a Logical NOT.
Swift Comments
(0190__Swift_Comments.htm)
let x = true
Swift for loop
let y = false
(0200__Swift_for.htm)
Swift while loop let c = !x
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0140__Swift_Logical_Operators.htm 1/2
13/02/2025, 11:57 Swift Tutorial - Swift Logical Operators
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0140__Swift_Logical_Operators.htm 2/2
13/02/2025, 11:57 Swift Tutorial - Swift Enumerations
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Enumerations
Swift Introduction (index.htm)
« Previous (0140__Swift_Logical_Operators.htm) Next » (0160__Swift_Arrays.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
You use an enumeration or an enum to define a restricted set of values.
(0050__Swift_Booleans.htm)
Swift Tuples The following code defines an enumeration for a thread state.
(0060__Swift_Tuples.htm)
Swift Optionals enum State {
(0070__Swift_Optionals.htm) case Inactive
Swift Global/Local Variables case Active
(0080__Swift_Global_Local_Variables.htm) case Hibernate
case Terminated
}
Operator
Swift Assertions
var machineState = State.Inactive
(0090__Swift_Assertions.htm)
Swift Assignment Operators machineState == State.Inactive ? println("Inactive") : println("State Unknown")
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) Example
To define an enumeration for the alphabet while keeping track of the position of each letter in the
Advanced Data Type
alphabet, use the code as follows.
Swift Enumerations ()
Swift Arrays
enum Alphabet:Int{
(0160__Swift_Arrays.htm)
case A = 1
Swift Dictionaries case B, C, D, E, F, G, H, I
(0170__Swift_Dictionaries.htm) case J, K, L, M, N, O, P, Q
case R, S, T, U, V, W, X, Y, Z
Statement }
Swift Comments
(0190__Swift_Comments.htm) To add the integer value of A to the integer value of B, use the code as follows.
Swift for loop
(0200__Swift_for.htm) let result = Alphabet.A.toRaw() + Alphabet.B.toRaw()
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm) « Previous (0140__Swift_Logical_Operators.htm) Next » (0160__Swift_Arrays.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0150__Swift_Enumerations.htm 1/2
13/02/2025, 11:57 Swift Tutorial - Swift Enumerations
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0150__Swift_Enumerations.htm 2/2
13/02/2025, 11:58 Swift Tutorial - Swift Arrays
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Arrays
Introduction « Previous (0150__Swift_Enumerations.htm) Next » (0170__Swift_Dictionaries.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
To organize a list of variables, we can use arrays.
(0040__Swift_String.htm)
Swift Booleans All items in an array must be of the same type.
(0050__Swift_Booleans.htm)
Swift Tuples
(0060__Swift_Tuples.htm) Example
Swift Optionals
(0070__Swift_Optionals.htm)
The following code shows two ways to create empty arrays that you can add items to later.
Swift Global/Local Variables
var a1:Array<String> = Array<String>()
(0080__Swift_Global_Local_Variables.htm)
var a2:[String] = [String]()
Operator
The first line shows the long way of declaring an array.
Swift Assertions
(0090__Swift_Assertions.htm) You must use the Array keyword and include the type that is stored in the array between <> .
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm) a1 is an array that can hold a list of strings.
Swift Arithmetic Operators
You must also use the assignment operator = to initialize the array followed by the word Array
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators along with the type between <> signs.
(0120__Swift_Comparison_Operators.htm)
The second line is a shorthand method for creating arrays.
Swift Range Operators
(0130__Swift_Range_Operators.htm) We can also use array items to initially create the array.
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) Swift supports type inference, and you can omit the initialization and the type declaration as
follows.
Advanced Data Type
Swift Enumerations var a3 = ["A", "B", "C"]
(0150__Swift_Enumerations.htm)
Swift Arrays ()
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Statement
Swift Comments Array Mutability
(0190__Swift_Comments.htm)
Swift for loop We can add, remove, and change the items in the array.
(0200__Swift_for.htm)
We can create immutable arrays with let keyword when creating the array.
Swift while loop
(0210__Swift_while.htm)
let a1 = ["D", "E", "F"]
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
Adding Items to Arrays
(0240__Swift_continue-break.htm) Arrays store items in a list that is indexed by integers starting with zero.
Function Use the += operator or the append function to add items to the end of an array.
Swift Functions
a1+="Apples"
(0300__Swift_Functions.htm)
a1.append("Oranges")
Swift Closures
(0310__Swift_Closures.htm)
a1 would now contain the two strings "Apples" and "Oranges".
Class/Structure To insert a new item to the middle of an array, use the insert function.
Swift Structures
(0320__Swift_Structures.htm) a1.insert("Swift", atIndex: 1)
www.java2s.com/Tutorials/Swift/Swift_Language/0160__Swift_Arrays.htm 1/2
13/02/2025, 11:58 Swift Tutorial - Swift Arrays
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
Removing Items from Arrays
(0360__Swift_Subscript.htm) To remove items from arrays, use removeAtIndex() .
Swift Inheritance
(0370__Swift_Inheritance.htm) Supply the index of the item to remove from the array.
Swift Initialization
(0380__Swift_Initialization.htm)
The following code shows an example that you can use to remove items from arrays.
Swift Type Casting
a1.removeAtIndex(0)
(0390__Swift_Type_Casting.htm)
a1.removeLast()
Swift Nested Types
a1.removeAll(keepCapacity: false)
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm) The parameter in removeAll(keepCapacity:) lets you indicate whether you want the array to
Swift Protocols stay initialized for the number of items that the array contained.
(0420__Swift_Protocols.htm)
Swift Generics
If you know you are replacing the items immediately, it could save processing resources if you
(0430__Swift_Generics.htm) leave the array initialized.
var a1 = [1, 2, 0, 4, 5]
a1[2] = 3
var a1 = [1, 2, 3, 4, 5]
for i in a4{
println("i = \(i)")
}
www.java2s.com/Tutorials/Swift/Swift_Language/0160__Swift_Arrays.htm 2/2
13/02/2025, 11:58 Swift Tutorial - Swift Dictionaries
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Dictionaries
Swift Introduction (index.htm)
« Previous (0160__Swift_Arrays.htm) Next » (0190__Swift_Comments.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
A dictionary stores key-value pair.
(0050__Swift_Booleans.htm)
Swift Tuples Arrays keep items in the order in which you put the items into the array, while dictionaries don't
(0060__Swift_Tuples.htm)
guarantee any order at all.
Swift Optionals
(0070__Swift_Optionals.htm)
Swift Global/Local Variables Example
(0080__Swift_Global_Local_Variables.htm)
To create a new dictionary, specify both the data type for the key and the data type for the value.
Operator
var d1:Dictionary<String, Int>
Swift Assertions
(0090__Swift_Assertions.htm)
Swift Assignment Operators d1 is declared as a dictionary that requires String keys and Int values.
(0100__Swift_Assignment_Operators.htm)
Before you add any items, you will need to initialize d1
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm) d1 = Dictionary()
Swift Comparison Operators A dictionary literal is an constant dictionary that is filled with an immutable collection of keys and
(0120__Swift_Comparison_Operators.htm)
values.
Swift Range Operators
(0130__Swift_Range_Operators.htm) var myDictionary = [1:"a",2:"b", 3:"c"]
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
The code above creates a dictionary called myDictionary that uses integers as keys and stores
web page addresses as string values.
Advanced Data Type
Swift Enumerations
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm)
Swift Dictionaries ()
Statement
Referencing Dictionary Items
Swift Comments To reference a dictionary item, supply the key enclosed in square brackets [] , after the
(0190__Swift_Comments.htm) dictionary name.
Swift for loop
(0200__Swift_for.htm) var myDictionary = [1:"a", 2:"b", 3:"c"]
Swift while loop
(0210__Swift_while.htm) let blogPage = myDictionary[2]
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm) Updating Dictionary Items
Swift continue break
(0240__Swift_continue-break.htm) You can update the value of a mutable dictionary by getting a reference to the item and using the
assignment operator to supply the new value.
Function
myDictionary[1] = "new value"
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures The following code shows how to use String and Float Keys
(0310__Swift_Closures.htm)
var d2:Dictionary = ["a":"AAAA", "b":"BBBB"]
var d3:Dictionary = [1.1:"AAAA", 1.2:"BBBB"]
Class/Structure
Swift Structures
d2["a"] = "string"
(0320__Swift_Structures.htm) d3[1.2] = "string"
www.java2s.com/Tutorials/Swift/Swift_Language/0170__Swift_Dictionaries.htm 1/2
13/02/2025, 11:58 Swift Tutorial - Swift Dictionaries
Swift Class To remove an item from a dictionary, simply update the item with the nil value or use the
(0330__Swift_Class.htm) removeValueForKey() function.
Swift Properties
(0340__Swift_Properties.htm) d2["b"] = nil
Swift Methods d2.removeValueForKey("a")
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting Iterating Over Dictionary Items
(0390__Swift_Type_Casting.htm)
To iterate over dictionary items, use the for in loop.
Swift Nested Types
(0400__Swift_Nested_Types.htm) You will get a reference to each key and item in the loop.
Swift Extensions
(0410__Swift_Extensions.htm) for (key, value) in d3{
Swift Protocols println("key:\(key), value:\(value)")
(0420__Swift_Protocols.htm) }
Swift Generics
(0430__Swift_Generics.htm) To iterate through the keys, access the key property on the dictionary.
www.java2s.com/Tutorials/Swift/Swift_Language/0170__Swift_Dictionaries.htm 2/2
13/02/2025, 11:58 Swift Tutorial - Swift Comments
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Comments
Swift Introduction (index.htm)
« Previous (0170__Swift_Dictionaries.htm) Next » (0200__Swift_for.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
We use code comments to add information to our code.
(0050__Swift_Booleans.htm)
Swift Tuples With Swift, you can add a one-line code comment, multiple-line code comments, and nested
(0060__Swift_Tuples.htm)
code comments.
Swift Optionals
(0070__Swift_Optionals.htm)
Swift Global/Local Variables One-Line Code Comments
(0080__Swift_Global_Local_Variables.htm)
Use the double backslashes // to start a one-line code comment.
Operator
//One line code comment
Swift Assertions
(0090__Swift_Assertions.htm)
Swift Assignment Operators You can fill up one line with these types of comments.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)
Multiline Comments
Swift Comparison Operators A multiline comment starts the comment with a backslash and asterisk /* , write the comment,
(0120__Swift_Comparison_Operators.htm)
and end it with an asterisk and backslash */ .
Swift Range Operators
(0130__Swift_Range_Operators.htm) /*
Swift Logical Operators Multiple line code comment
(0140__Swift_Logical_Operators.htm) Use this when you need to provide more detail
*/
Advanced Data Type
Swift Enumerations
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm)
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Nested Code Comments
Statement
Swift Comments ()
Swift supports nested comments.
Swift for loop
/*
(0200__Swift_for.htm)
/*
Swift while loop
Define a and b constants
(0210__Swift_while.htm)
*/
Swift if (0220__Swift_if.htm)
Swift switch let a = 1
(0230__Swift_switch.htm) let b = 2
Swift continue break
(0240__Swift_continue-break.htm) /*
Define c and d constants
Function */
Swift Functions
let c = 3
(0300__Swift_Functions.htm)
let d = 4
Swift Closures
*/
(0310__Swift_Closures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0190__Swift_Comments.htm 1/2
13/02/2025, 11:58 Swift Tutorial - Swift Comments
Swift Class « Previous (0170__Swift_Dictionaries.htm) Next » (0200__Swift_for.htm)
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0190__Swift_Comments.htm 2/2
13/02/2025, 11:58 Swift Tutorial - Swift for statement
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift for
Introduction
statement
Swift Introduction (index.htm)
« Previous (0190__Swift_Comments.htm) Next » (0210__Swift_while.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
The for loop executes a set number of times.
(0050__Swift_Booleans.htm)
Swift Tuples We use the for keyword along with an ending condition and a for-loop statement.
(0060__Swift_Tuples.htm)
Swift Optionals
(0070__Swift_Optionals.htm) Example
Swift Global/Local Variables
The following loop statement prints out 1 through 10 to the console.
(0080__Swift_Global_Local_Variables.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0200__Swift_for.htm 1/2
13/02/2025, 11:58 Swift Tutorial - Swift for statement
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
Nested for loop with continue
(0350__Swift_Methods.htm) If you wanted to skip any value where y equals 2, you could just include a continue statement
Swift Subscripts
right inside the innermost loop.
(0360__Swift_Subscript.htm)
Swift Inheritance
for x in 1...3 {
(0370__Swift_Inheritance.htm)
for y in 1...3 {
Swift Initialization
if y == 2{
(0380__Swift_Initialization.htm) continue
Swift Type Casting }
(0390__Swift_Type_Casting.htm) println("x = \(x), y = \(y)")
Swift Nested Types }
(0400__Swift_Nested_Types.htm) }
Swift Extensions
(0410__Swift_Extensions.htm) The code above generates the following result.
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
for-in Loop
Use the for in loop to iterate over items in collections such as arrays and dictionaries.
To use a for in loop, specify a local variable name and the collection you are iterating over.
for n in names{
println(n)
}
var result = 0
for i in 1...3{
++result
}
The above code 3 uses a range operator to specify a range between 1 and 3 and uses this to
increment the variable result.
www.java2s.com/Tutorials/Swift/Swift_Language/0200__Swift_for.htm 2/2
13/02/2025, 11:58 Swift Tutorial - Swift while loop
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift while loop
Introduction « Previous (0200__Swift_for.htm) Next » (0220__Swift_if.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
while loops work by evaluating a condition and executing code as long as the condition is true.
(0040__Swift_String.htm)
Swift Booleans You write a while loop by including the while keyword followed by the condition and the code
(0050__Swift_Booleans.htm)
block to execute.
Swift Tuples
(0060__Swift_Tuples.htm)
Swift Optionals Example
(0070__Swift_Optionals.htm)
Swift Global/Local Variables var i = 1
(0080__Swift_Global_Local_Variables.htm)
while i <= 10{
Operator println("i = \(i)")
i++
Swift Assertions
}
(0090__Swift_Assertions.htm)
Swift Assignment Operators
The code above generates the following result.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
do-while Loop
Swift for loop The do-while loop will execute the code in the block before evaluating the condition.
(0200__Swift_for.htm)
Swift while loop () Use this loop when you want to make sure that code executes at least one time.
Swift if (0220__Swift_if.htm)
Swift switch do{
(0230__Swift_switch.htm) println("i = \(i)")
i++
Swift continue break
}while i <= 10
(0240__Swift_continue-break.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0210__Swift_while.htm 1/2
13/02/2025, 11:58 Swift Tutorial - Swift while loop
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0210__Swift_while.htm 2/2
13/02/2025, 11:59 Swift Tutorial - Swift if Statement
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift if
Introduction
Statement
Swift Introduction (index.htm)
« Previous (0210__Swift_while.htm) Next » (0230__Swift_switch.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
if statements are used to make a choice to execute code based on the result of a comparison
(0050__Swift_Booleans.htm)
Swift Tuples expression.
(0060__Swift_Tuples.htm)
Swift Optionals if (1 < 2){
println("This is true")
(0070__Swift_Optionals.htm)
}
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
In the code above if 1 is less than 2, then execute the code that will print out the string "That is
Operator true" to the console log.
Swift Assertions
(0090__Swift_Assertions.htm)
Swift Assignment Operators
else Keyword
(0100__Swift_Assignment_Operators.htm)You can also define an alternate action with the else keyword.
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)You execute either one of two actions based on the results of the expression.
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)if (1 < 2){
println("That is true")
Swift Range Operators
} else {
(0130__Swift_Range_Operators.htm)
println("Not true")
Swift Logical Operators
}
(0140__Swift_Logical_Operators.htm)
The code above will print out the text "That is true" to the console log since 1 is always less than
Advanced Data Type
2.
Swift Enumerations
(0150__Swift_Enumerations.htm) The following code shows how to use Nested if Statements.
Swift Arrays
(0160__Swift_Arrays.htm) if (1 > 2){
Swift Dictionaries println("True")
(0170__Swift_Dictionaries.htm) } else {
if (3 > 4){
Statement println("True")
} else {
Swift Comments
println("Not True")
(0190__Swift_Comments.htm)
}
Swift for loop
}
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if ()
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm) « Previous (0210__Swift_while.htm) Next » (0230__Swift_switch.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0220__Swift_if.htm 1/2
13/02/2025, 11:59 Swift Tutorial - Swift if Statement
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0220__Swift_if.htm 2/2
13/02/2025, 11:59 Swift Tutorial - Swift switch Statement
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift switch
Introduction
Statement
Swift Introduction (index.htm)
« Previous (0220__Swift_if.htm) Next » (0240__Swift_continue-break.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
switch statements are used to execute code based on the value of a variable.
(0050__Swift_Booleans.htm)
Swift Tuples
(0060__Swift_Tuples.htm) Example
Swift Optionals
(0070__Swift_Optionals.htm) var shape = 1
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
switch shape {
case 0:
Operator println("0")
Swift Assertions
case 1:
(0090__Swift_Assertions.htm)
println("1")
Swift Assignment Operators default:
(0100__Swift_Assignment_Operators.htm) println("No Shape Specified")
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm) }
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
}
Function
Swift Functions
(0300__Swift_Functions.htm)
When a case condition is made, code execution stops in the switch and resumes right after the
Swift Closures ending curly bracket } .
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0230__Swift_switch.htm 1/2
13/02/2025, 11:59 Swift Tutorial - Swift switch Statement
Swift Class
(0330__Swift_Class.htm)
fallthrough
Swift Properties If you would like to code switch statements that do include this fall-through behavior, you can
(0340__Swift_Properties.htm)
use the fallthrough statement.
Swift Methods
(0350__Swift_Methods.htm)
switch x {
Swift Subscripts
(0360__Swift_Subscript.htm)
case 0...5:
Swift Inheritance println("0 through 5")
(0370__Swift_Inheritance.htm) case 6:
Swift Initialization fallthrough
(0380__Swift_Initialization.htm) case 7:
Swift Type Casting println("Value 6 or 7")
(0390__Swift_Type_Casting.htm) default:
Swift Nested Types println("Default")
(0400__Swift_Nested_Types.htm) }
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
« Previous (0220__Swift_if.htm) Next » (0240__Swift_continue-break.htm)
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0230__Swift_switch.htm 2/2
13/02/2025, 11:59 Swift Tutorial - Swift continue break
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift continue
Introduction
break
Swift Introduction (index.htm)
« Previous (0230__Swift_switch.htm) Next » (0300__Swift_Functions.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
(0050__Swift_Booleans.htm)
Swift Tuples continue Statement
(0060__Swift_Tuples.htm)
Swift Optionals continue statement stops code execution and return to the beginning of the loop's code block.
(0070__Swift_Optionals.htm)
Swift Global/Local Variables for i in 1...10 {
if (i >=4 && i <= 7){
(0080__Swift_Global_Local_Variables.htm)
continue
}
Operator println(i)
Swift Assertions }
(0090__Swift_Assertions.htm)
Swift Assignment Operators The code above generates the following result.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Advanced Data Type The numbers 4 through 7 are not printed because of the continue statement.
Swift Enumerations
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm)
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
break Statement
Statement break statement completely interrupts the execution of a loop.
Swift Comments
(0190__Swift_Comments.htm) for i in 1...10 {
Swift for loop if (i >=4 && i <= 7){
(0200__Swift_for.htm) break
Swift while loop }
(0210__Swift_while.htm) println(i)
Swift if (0220__Swift_if.htm) }
Swift switch
(0230__Swift_switch.htm)
Swift continue break () The code above generates the following result.
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm) You can use break statements in switch statements.
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0240__Swift_continue-break.htm 1/2
13/02/2025, 11:59 Swift Tutorial - Swift continue break
Swift Class
(0330__Swift_Class.htm)
Labeled Statements
Swift Properties You can use the break continue , and fallthrough control statements with labeled
(0340__Swift_Properties.htm)
statements.
Swift Methods
(0350__Swift_Methods.htm)
outerloop: for x in 1...3 {
Swift Subscripts
innerloop: for y in 1...3 {
(0360__Swift_Subscript.htm)
if y == 2{
Swift Inheritance continue outerloop
(0370__Swift_Inheritance.htm) }
Swift Initialization println("x = \(x), y = \(y)")
(0380__Swift_Initialization.htm) }
Swift Type Casting }
(0390__Swift_Type_Casting.htm)
Swift Nested Types The code above generates the following result.
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
« Previous (0230__Swift_switch.htm) Next » (0300__Swift_Functions.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0240__Swift_continue-break.htm 2/2
13/02/2025, 11:59 Swift Tutorial - Swift Functions
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Functions
Introduction « Previous (0240__Swift_continue-break.htm) Next » (0310__Swift_Closures.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
You can declare a function with the func keyword.
(0040__Swift_String.htm)
Swift Booleans Functions organizes code into reusable chunks that take input parameters and return results.
(0050__Swift_Booleans.htm)
Swift Tuples The following code shows an example of a function that returns the string "Hello World."
(0060__Swift_Tuples.htm)
Swift Optionals func myFunction() -> String{
(0070__Swift_Optionals.htm) return "Hello World"
Swift Global/Local Variables }
let s = myFunction();
(0080__Swift_Global_Local_Variables.htm)
println(s)
//Calling Functions Directly
Operator println(myFunction())
Swift Assertions
(0090__Swift_Assertions.htm) In the code above, the function declaration starts with the func keyword followed by the name of
Swift Assignment Operators
the function.
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators The following code shows a function that just writes out the log without returning a value.
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators func myFunction() {
(0120__Swift_Comparison_Operators.htm) println("Printing myFunction() function")
Swift Range Operators return
(0130__Swift_Range_Operators.htm) }
Swift Logical Operators myFunction()
(0140__Swift_Logical_Operators.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0300__Swift_Functions.htm 1/2
13/02/2025, 11:59 Swift Tutorial - Swift Functions
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
Nested Functions
(0360__Swift_Subscript.htm) You can code functions within other functions.
Swift Inheritance
(0370__Swift_Inheritance.htm) Nesting functions can organize and reuse our code while limiting the scope of functions to the
Swift Initialization parent function.
(0380__Swift_Initialization.htm)
Swift Type Casting func myFunction(){
(0390__Swift_Type_Casting.htm) func calculate(scores:[Int]) -> Float?{
Swift Nested Types if scores.count > 0 {
(0400__Swift_Nested_Types.htm) var total:Int = 0
Swift Extensions var count:Int = 0
(0410__Swift_Extensions.htm) for score in scores{
Swift Protocols total+=score
count++
(0420__Swift_Protocols.htm)
}
Swift Generics
return (Float)(total / count)
(0430__Swift_Generics.htm)
}else {
return nil
}
}
func printReport(testName:String, scores:[Int]){
if let a = calculate(scores){
println("\(testName) Test Results")
println(" The average score is \(a)")
}
}
printReport("Math", [9, 4, 2, 6, 5, 5, 3, 9])
printReport("Java", [3, 7, 2, 5])
myFunction()
www.java2s.com/Tutorials/Swift/Swift_Language/0300__Swift_Functions.htm 2/2
13/02/2025, 11:59 Swift Tutorial - Swift Closures
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Closures
Introduction « Previous (0300__Swift_Functions.htm) Next » (0320__Swift_Structures.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
Closures are blocks of code that you can pass to functions as parameters or store as variables
(0040__Swift_String.htm)
Swift Booleans or constants.
(0050__Swift_Booleans.htm)
Closures capture the state of the other variables around them.
Swift Tuples
(0060__Swift_Tuples.htm)
var alpha = ["D", "E", "A", "C", "B"]
Swift Optionals
let alpha_sorted = sorted(alpha, { (s1: String, s2: String) -> Bool in
(0070__Swift_Optionals.htm) return s1 < s2
Swift Global/Local Variables })
(0080__Swift_Global_Local_Variables.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures ()
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0310__Swift_Closures.htm 1/2
13/02/2025, 11:59 Swift Tutorial - Swift Closures
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0310__Swift_Closures.htm 2/2
13/02/2025, 12:01 Swift Tutorial - Swift Structures
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Structures
Introduction « Previous (0310__Swift_Closures.htm) Next » (0330__Swift_Class.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
You can use structures to define custom types in Swift.
(0040__Swift_String.htm)
Swift Booleans Structures give you a way of grouping related information together.
(0050__Swift_Booleans.htm)
Swift Tuples To define a Swift structure, use the struct keyword.
(0060__Swift_Tuples.htm)
Swift Optionals struct Rectangle {
(0070__Swift_Optionals.htm) var x:Int = 0
Swift Global/Local Variables var y:Int = 0
(0080__Swift_Global_Local_Variables.htm) var width:Int = 0
var height:Int = 0
}
Operator
Swift Assertions
(0090__Swift_Assertions.htm)
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators Structure Instances
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
To use a structure type, you must create a new instance based on this definition.
(0130__Swift_Range_Operators.htm)
struct Rectangle {
Swift Logical Operators
var x:Int = 0
(0140__Swift_Logical_Operators.htm)
var y:Int = 0
var width:Int = 0
Advanced Data Type var height:Int = 0
Swift Enumerations }
(0150__Swift_Enumerations.htm)
Swift Arrays var rect = Rectangle()
(0160__Swift_Arrays.htm)
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop Accessing Structure Properties
(0200__Swift_for.htm)
Swift while loop To access structure properties, you use dot syntax.
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm) rect.x = 10
Swift switch rect.y = 10
rect.width = 100
(0230__Swift_switch.htm)
rect.height = 50
Swift continue break
(0240__Swift_continue-break.htm)
println("x: \(rect.x), y: \(rect.y), width: \(rect.width),height: \(rect.height)")
Function Structures are value types. This means members are copied and not passed by reference when
Swift Functions
the assignment statement is used.
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm) Structure Functions
Class/Structure Swift structure types can have functions as part of their definition.
Swift Structures () This means Swift structures not only can organize data but can define behavior.
www.java2s.com/Tutorials/Swift/Swift_Language/0320__Swift_Structures.htm 1/2
13/02/2025, 12:01 Swift Tutorial - Swift Structures
Swift Class The following code adds a function to a structure.
(0330__Swift_Class.htm)
Swift Properties struct Rectangle {
(0340__Swift_Properties.htm) var x:Int = 0
Swift Methods var y:Int = 0
(0350__Swift_Methods.htm) var width:Int = 0
Swift Subscripts var height:Int = 0
(0360__Swift_Subscript.htm)
Swift Inheritance func description() -> String{
return ("x: \(x), y: \(y), width: \(width), height: \(height)")
(0370__Swift_Inheritance.htm)
}
Swift Initialization
(0380__Swift_Initialization.htm)
}
Swift Type Casting
var rect = Rectangle()
(0390__Swift_Type_Casting.htm) rect.x = 10
Swift Nested Types rect.y = 10
(0400__Swift_Nested_Types.htm) rect.width = 100
Swift Extensions rect.height = 50
(0410__Swift_Extensions.htm)
Swift Protocols println(rect.description())
(0420__Swift_Protocols.htm)
Swift Generics The code above generates the following result.
(0430__Swift_Generics.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0320__Swift_Structures.htm 2/2
13/02/2025, 12:01 Swift Tutorial - Swift Class
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Class
Introduction « Previous (0320__Swift_Structures.htm) Next » (0340__Swift_Properties.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
We can use classes to define types in Swift.
(0040__Swift_String.htm)
Swift Booleans Classes give us a way of grouping related information and behavior together.
(0050__Swift_Booleans.htm)
Swift Tuples To define a Swift class, use the class keyword.
(0060__Swift_Tuples.htm)
Swift Optionals class Person {
(0070__Swift_Optionals.htm) var name: String = "Name"
Swift Global/Local Variables var age:Int = 0
(0080__Swift_Global_Local_Variables.htm)
func profile() -> String {
return "I'm \(self.name) and I'm \(self.age) years old."
Operator }
Swift Assertions
(0090__Swift_Assertions.htm) }
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm)In the code above we defined a class named Person .
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)The Person class consists of a string for a person's name and an integer for a person's age.
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)These variable and constant types are called properties.
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Function
Accessing Class Properties
Swift Functions For class instance properties you use dot syntax.
(0300__Swift_Functions.htm)
Swift Closures p.name = "Tom"
(0310__Swift_Closures.htm) p.age = 40
Class/Structure In the code above we use dot syntax to change the variable values.
Swift Structures
Classes are reference types.
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0330__Swift_Class.htm 1/3
13/02/2025, 12:01 Swift Tutorial - Swift Class
Swift Class () When you use an assignment operator, you assign only a reference to the instance.
Swift Properties
(0340__Swift_Properties.htm) When changing a class instance members, they will reflected in the instance everywhere in the
Swift Methods program.
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm) Class and Structure Instances
Swift Inheritance
struct S {
(0370__Swift_Inheritance.htm)
var i = 1
Swift Initialization
}
(0380__Swift_Initialization.htm)
Swift Type Casting
class C {
(0390__Swift_Type_Casting.htm) var i = 1
Swift Nested Types }
(0400__Swift_Nested_Types.htm)
Swift Extensions var s = S()
(0410__Swift_Extensions.htm)
Swift Protocols var c = C()
(0420__Swift_Protocols.htm)
Swift Generics println("s.i = \(s.i)")
(0430__Swift_Generics.htm) println("c.i = \(c.i)")
s.i = 1
c.i = 1
For a structure, you get a copy of the instance during the assignment. This copy acts
independently from the original instance. Changing the copy will not be reflected in the original
instance.
Assigning a variable to a class instance makes changes to the class instance members, the
original instance content will also change.
struct S {
var i = 1
}
class C {
var i = 1
}
var s2 = s
var c2 = c
s2.i = 2
c2.i = 2
println("s.i = \(s.i)")
println("c.i = \(c.i)")
From the output we can see that the original structure member didn't change while the original
class instance member's value changed to be the same value as the i property on c2 .
This operator returns true if the instance variables both point to the same instance.
www.java2s.com/Tutorials/Swift/Swift_Language/0330__Swift_Class.htm 2/3
13/02/2025, 12:01 Swift Tutorial - Swift Class
class Person {
var name: String = "Name"
var age:Int = 0
}
var p1 = Person()
p1.name = "A"
p1.age = 40
var p2 = Person()
p2.name = "B"
p2.age = 25
var b1 = p2 === p2
var b2 = p1 === p2
www.java2s.com/Tutorials/Swift/Swift_Language/0330__Swift_Class.htm 3/3
13/02/2025, 12:02 Swift Tutorial - Swift Properties
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Properties
Introduction « Previous (0330__Swift_Class.htm) Next » (0350__Swift_Methods.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
We can use properties to describe attributes of an object.
(0040__Swift_String.htm)
Swift Booleans To add a property to an object in Swift, add a variable or constant declaration to the type
(0050__Swift_Booleans.htm)
definition.
Swift Tuples
(0060__Swift_Tuples.htm) Properties are not limited to class definitions in Swift. Enumerations and structures can also have
Swift Optionals
property declarations.
(0070__Swift_Optionals.htm)
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)Example
Operator
class Person {
Swift Assertions
var name: String = "Name"
(0090__Swift_Assertions.htm)
var age:Int = 0
Swift Assignment Operators
func profile() -> String {
(0100__Swift_Assignment_Operators.htm) return "I'm \(self.name) and I'm \(self.age) years old."
Swift Arithmetic Operators }
(0110__Swift_Arithmetic_Operators.htm) }
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)var p = Person()
Swift Range Operators
(0130__Swift_Range_Operators.htm) p.name = "Jack"
Swift Logical Operators p.age = 4
(0140__Swift_Logical_Operators.htm)
println("p.name = \(p.name)")
println("p.age = \(p.age)")
Advanced Data Type
Swift Enumerations
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm)
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0340__Swift_Properties.htm 1/3
13/02/2025, 12:02 Swift Tutorial - Swift Properties
Swift Class
(0330__Swift_Class.htm)
Swift Properties ()
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Lazy Properties
Swift Inheritance A lazy stored property's value will not be generated until the first time the property value is
(0370__Swift_Inheritance.htm)
used.
Swift Initialization
(0380__Swift_Initialization.htm) Lazy Properties are used when the property represents a resource that may be expensive to
Swift Type Casting
generate and needed in only specific situations.
(0390__Swift_Type_Casting.htm)
Swift Nested Types To make a property a lazy stored property, you use the lazy modifier.
(0400__Swift_Nested_Types.htm)
Swift Extensions For instance, the following code shows how you would make the name property of the Person
(0410__Swift_Extensions.htm) class a lazy stored property.
Swift Protocols
(0420__Swift_Protocols.htm) class Person {
Swift Generics lazy var name: String = "Name"
(0430__Swift_Generics.htm) var age:Int = 0
func profile() -> String {
return "I'm \(self.name) and I'm \(self.age) years old."
}
}
var p = Person()
p.name = "Tom"
p.age = 40
Computed Properties
Computed properties take input values and return a new result.
class Person {
lazy var name: String = "Name"
var age:Int = 0
var profile:String{
get{
return "I'm \(self.name) and I'm \(self.age) years old."
}
}
var p = Person()
p.name = "Tom"
p.age = 40
p.profile
Property Observers
We can listen to the change event of a property with the willSet and didSet keywords.
These keywords in the property declaration assigns an action handlers which is called when a
property is about to change and when a property value was just changed.
www.java2s.com/Tutorials/Swift/Swift_Language/0340__Swift_Properties.htm 2/3
13/02/2025, 12:02 Swift Tutorial - Swift Properties
class Person {
var name: String = "Name"
var age:Int = 0{
willSet{
println("age is about to change to \(newValue)")
}
didSet{
println("age was just changed to \(self.age)")
}
}
func profile() -> String {
return "I'm \(self.name) and I'm \(self.age) years old."
}
}
var p = Person()
p.age = 40
class Person {
class var species:String{
return "Human"
}
}
Person.species
Use the static keyword to declare a type property for an enumeration or a structure.
struct Rectangle {
var x:Int = 0
var y:Int = 0
var width:Int = 0
var height:Int = 0
Rectangle.gtp
We can access type property values by referencing the type name and using dot syntax to
access the value.
We can treat type properties for value types such as enumerations and structures in the same
way that we do for instance properties.
print(Rectangle.gtp)
www.java2s.com/Tutorials/Swift/Swift_Language/0340__Swift_Properties.htm 3/3
13/02/2025, 12:02 Swift Tutorial - Swift Methods
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Methods
Introduction « Previous (0340__Swift_Properties.htm) Next » (0360__Swift_Subscript.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans Type Methods
(0050__Swift_Booleans.htm)
Swift Tuples Class functions are also called as methods.
(0060__Swift_Tuples.htm)
Instance methods require a type instance.
Swift Optionals
(0070__Swift_Optionals.htm) Type methods are functions that require the type (class, enumeration, or structure) to work.
Swift Global/Local Variables
To declare a type method for a class, you must use the class keyword.
(0080__Swift_Global_Local_Variables.htm)
class Person {
Operator
class var species:String{
Swift Assertions return "Human"
(0090__Swift_Assertions.htm) }
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm) class func printDescription() {
Swift Arithmetic Operators println( "Species: \(species).")
(0110__Swift_Arithmetic_Operators.htm) }
Swift Comparison Operators }
(0120__Swift_Comparison_Operators.htm)Person.printDescription()
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Function
Swift Functions « Previous (0340__Swift_Properties.htm) Next » (0360__Swift_Subscript.htm)
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0350__Swift_Methods.htm 1/2
13/02/2025, 12:02 Swift Tutorial - Swift Methods
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods ()
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0350__Swift_Methods.htm 2/2
13/02/2025, 12:02 Swift Tutorial - Swift Subscripts
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Subscripts
Introduction « Previous (0350__Swift_Methods.htm) Next » (0370__Swift_Inheritance.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
A subscript is a key that is used to extract a value from a collection. For example, we use the
(0040__Swift_String.htm)
Swift Booleans subscript in array or dictionary.
(0050__Swift_Booleans.htm)
In Swift we can add subscript to our own types.
Swift Tuples
(0060__Swift_Tuples.htm)
Swift Optionals Example
(0070__Swift_Optionals.htm)
Swift Global/Local Variables The following code shows how to add subscript to Person class.
(0080__Swift_Global_Local_Variables.htm)
class Person {
private var roles = ["Manager", "Coder", "Designer"]
Operator
Swift Assertions subscript(index: Int) -> String {
(0090__Swift_Assertions.htm) get {
Swift Assignment Operators return roles[index]
(0100__Swift_Assignment_Operators.htm) }
Swift Arithmetic Operators set(newValue) {
(0110__Swift_Arithmetic_Operators.htm) self.roles[index] = newValue
Swift Comparison Operators }
(0120__Swift_Comparison_Operators.htm) }
Swift Range Operators }
(0130__Swift_Range_Operators.htm)
var p = Person()
Swift Logical Operators
println(p[1])
(0140__Swift_Logical_Operators.htm)
p[0] = "Tester"
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0360__Swift_Subscript.htm 1/2
13/02/2025, 12:02 Swift Tutorial - Swift Subscripts
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts ()
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0360__Swift_Subscript.htm 2/2
13/02/2025, 12:02 Swift Tutorial - Swift Inheritance
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Inheritance
Swift Introduction (index.htm)
« Previous (0360__Swift_Subscript.htm) Next » (0380__Swift_Initialization.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
Classes have the ability to inherit methods and properties from a parent class.
(0050__Swift_Booleans.htm)
Swift Tuples
class Person {
(0060__Swift_Tuples.htm)
var name: String = "Name"
Swift Optionals
var age:Int = 0
(0070__Swift_Optionals.htm)
Swift Global/Local Variables func profile() -> String {
(0080__Swift_Global_Local_Variables.htm) return "I'm \(self.name) and I'm \(self.age) years old."
}
Operator }
Swift Assertions
class Employee: Person {
(0090__Swift_Assertions.htm)
Swift Assignment Operators
}
(0100__Swift_Assignment_Operators.htm)
Swift Arithmetic Operators
In the code above, Employee is inheriting from Person .
(0110__Swift_Arithmetic_Operators.htm)
Swift Comparison Operators To indicate what class to inherit, add a colon : and the name of the parent class.
(0120__Swift_Comparison_Operators.htm)
Swift Range Operators
(0130__Swift_Range_Operators.htm)
Swift Logical Operators
(0140__Swift_Logical_Operators.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0370__Swift_Inheritance.htm 1/3
13/02/2025, 12:02 Swift Tutorial - Swift Inheritance
Swift Class
class Person {
(0330__Swift_Class.htm) var name: String = "Name"
Swift Properties var age:Int = 0
(0340__Swift_Properties.htm) func profile() -> String {
Swift Methods return "I'm \(self.name) and I'm \(self.age) years old."
(0350__Swift_Methods.htm) }
Swift Subscripts }
(0360__Swift_Subscript.htm)
Swift Inheritance () class Employee: Person {
Swift Initialization var employeeNumber = 1234567890
(0380__Swift_Initialization.htm) var hourlyRate = 12.00
override func profile() -> String {
Swift Type Casting
return "I'm \(self.name) and my hourly rate is $\(self.hourlyRate)"
(0390__Swift_Type_Casting.htm)
}
Swift Nested Types
(0400__Swift_Nested_Types.htm)
}
Swift Extensions
(0410__Swift_Extensions.htm) var p1 = Person()
Swift Protocols
(0420__Swift_Protocols.htm) p1.name = "Jack"
Swift Generics p1.age = 40
(0430__Swift_Generics.htm)
println(p1.profile())
var e1 = Employee()
e1.name = "Jodd"
e1.age = 18
e1.employeeNumber = 1
e1.hourlyRate = 45.55
println(e1.profile())
Override Properties
We can also override property declarations.
www.java2s.com/Tutorials/Swift/Swift_Language/0370__Swift_Inheritance.htm 2/3
13/02/2025, 12:02 Swift Tutorial - Swift Inheritance
class Person {
var name: String = "Name"
var age:Int = 0
func profile() -> String {
return "I'm \(self.name) and I'm \(self.age) years old."
}
var lastName:String {
get {
return "NoName"
}
set {
_lastName = newValue
}
}
}
class Employee: Person {
var employeeNumber = 2
var hourlyRate = 12.00
var p1 = Person()
p1.lastName = "A"
var e1 = Employee()
e1.lastName = "B"
println(p1.lastName)
println(e1.lastName)
www.java2s.com/Tutorials/Swift/Swift_Language/0370__Swift_Inheritance.htm 3/3
13/02/2025, 12:02 Swift Tutorial - Swift Initialization
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Initialization
Swift Introduction (index.htm)
« Previous (0370__Swift_Inheritance.htm) Next » (0390__Swift_Type_Casting.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
We initialize an instance to get the instance ready for use.
(0050__Swift_Booleans.htm)
Swift Tuples
class Person {
(0060__Swift_Tuples.htm)
var name: String = "Name"
Swift Optionals
var age:Int = 0
(0070__Swift_Optionals.htm) }
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)
var p = Person()
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0380__Swift_Initialization.htm 1/2
13/02/2025, 12:02 Swift Tutorial - Swift Initialization
Swift Class
class Person {
(0330__Swift_Class.htm) var name: String
Swift Properties var age:Int
(0340__Swift_Properties.htm)
Swift Methods init() {
(0350__Swift_Methods.htm) self.name = "Name"
Swift Subscripts self.age = 0
(0360__Swift_Subscript.htm) }
Swift Inheritance
(0370__Swift_Inheritance.htm) init(name:String, age:Int) {
Swift Initialization () self.name = name
self.age = age
Swift Type Casting
}
(0390__Swift_Type_Casting.htm)
Swift Nested Types
}
(0400__Swift_Nested_Types.htm)
var p = Person(name: "Jack", age: 4)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
De-initialization
Swift Generics When an instance is no longer need, Swift deallocates the instance and frees up the instance
(0430__Swift_Generics.htm) resources.
class Person {
deinit {
//release any resources
}
}
www.java2s.com/Tutorials/Swift/Swift_Language/0380__Swift_Initialization.htm 2/2
13/02/2025, 12:02 Swift Tutorial - Swift Type Casting
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Type
Introduction
Casting
Swift Introduction (index.htm)
« Previous (0380__Swift_Initialization.htm) Next » (0400__Swift_Nested_Types.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
Type casting is to convert value types such as floating-point and integer numbers to other types
(0050__Swift_Booleans.htm)
Swift Tuples in Swift.
(0060__Swift_Tuples.htm)
The following code shows some examples of how you might convert a floating-point number to
Swift Optionals
(0070__Swift_Optionals.htm)
other value types.
Swift Global/Local Variables
let f1 = 9.99
(0080__Swift_Global_Local_Variables.htm)
let i1 = Int(f1)
let d1 = Double(f1)
Operator let b1 = Bool(f1)
Swift Assertions let s1 = toString(f1)
(0090__Swift_Assertions.htm)
Swift Assignment Operators
(0100__Swift_Assignment_Operators.htm) Type Casting Instances
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)We can type cast objects using the as keyword.
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)class Person {
Swift Range Operators var name: String = "Name"
(0130__Swift_Range_Operators.htm) func printPerson() {
println("person")
Swift Logical Operators
}
(0140__Swift_Logical_Operators.htm)
}
Statement e1.printPerson()
Swift Comments e1.printEmployee()
(0190__Swift_Comments.htm)
Swift for loop let a1 = [e1]
(0200__Swift_for.htm)
Swift while loop for item in a1 {
(0210__Swift_while.htm) let p = item as Person
p.printPerson()
Swift if (0220__Swift_if.htm)
}
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
« Previous (0380__Swift_Initialization.htm) Next » (0400__Swift_Nested_Types.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0390__Swift_Type_Casting.htm 1/2
13/02/2025, 12:02 Swift Tutorial - Swift Type Casting
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting ()
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0390__Swift_Type_Casting.htm 2/2
13/02/2025, 12:03 Swift Tutorial - Swift Nested Types
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Nested
Introduction
Types
Swift Introduction (index.htm)
« Previous (0390__Swift_Type_Casting.htm) Next » (0410__Swift_Extensions.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
We can nest other types in our type definitions.
(0050__Swift_Booleans.htm)
Swift Tuples
(0060__Swift_Tuples.htm) Example
Swift Optionals
(0070__Swift_Optionals.htm) The following code creates a Person with nested class.
Swift Global/Local Variables
class Person {
(0080__Swift_Global_Local_Variables.htm)
var health = Health()
struct Shapes {
Statement struct Line {
Swift Comments var x:Int = 0
(0190__Swift_Comments.htm) var y:Int = 0
Swift for loop var length:Int = 0
(0200__Swift_for.htm) }
Swift while loop
(0210__Swift_while.htm) struct Rectangle {
Swift if (0220__Swift_if.htm) var x:Int = 0
var y:Int = 0
Swift switch
var width:Int = 0
(0230__Swift_switch.htm)
var height:Int = 0
Swift continue break
var area:Int = 0
(0240__Swift_continue-break.htm)
}
(0310__Swift_Closures.htm)
}
www.java2s.com/Tutorials/Swift/Swift_Language/0400__Swift_Nested_Types.htm 1/2
13/02/2025, 12:03 Swift Tutorial - Swift Nested Types
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm) « Previous (0390__Swift_Type_Casting.htm) Next » (0410__Swift_Extensions.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types ()
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0400__Swift_Nested_Types.htm 2/2
13/02/2025, 12:03 Swift Tutorial - Swift Extensions
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift
Introduction
Extensions
Swift Introduction (index.htm)
« Previous (0400__Swift_Nested_Types.htm) Next » (0420__Swift_Protocols.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
(0040__Swift_String.htm)
Swift Booleans
We can use extensions to add methods and computed properties to an existing class,
(0050__Swift_Booleans.htm)
Swift Tuples structure, or enumeration.
(0060__Swift_Tuples.htm)
Swift Optionals
(0070__Swift_Optionals.htm)
Example
Swift Global/Local Variables The following code shows how to extend the Person Class
(0080__Swift_Global_Local_Variables.htm)
class Person {
Operator var age:Int = 0
}
Swift Assertions
(0090__Swift_Assertions.htm)
extension Person {
Swift Assignment Operators
var yearsToRetire:Int {
(0100__Swift_Assignment_Operators.htm) get{
Swift Arithmetic Operators return 65 - self.age
(0110__Swift_Arithmetic_Operators.htm) }
Swift Comparison Operators }
(0120__Swift_Comparison_Operators.htm)}
Swift Range Operators
(0130__Swift_Range_Operators.htm) var p = Person()
Swift Logical Operators p.age = 40
(0140__Swift_Logical_Operators.htm)
println(p.yearsToRetire)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch
(0230__Swift_switch.htm)
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0410__Swift_Extensions.htm 1/2
13/02/2025, 12:03 Swift Tutorial - Swift Extensions
Swift Class
(0330__Swift_Class.htm)
Swift Properties
(0340__Swift_Properties.htm)
Swift Methods
(0350__Swift_Methods.htm)
Swift Subscripts
(0360__Swift_Subscript.htm)
Swift Inheritance
(0370__Swift_Inheritance.htm)
Swift Initialization
(0380__Swift_Initialization.htm)
Swift Type Casting
(0390__Swift_Type_Casting.htm)
Swift Nested Types
(0400__Swift_Nested_Types.htm)
Swift Extensions ()
Swift Protocols
(0420__Swift_Protocols.htm)
Swift Generics
(0430__Swift_Generics.htm)
java2s.com (http://www.java2s.com/) | © Demo Source and Support. All rights reserved.
www.java2s.com/Tutorials/Swift/Swift_Language/0410__Swift_Extensions.htm 2/2
13/02/2025, 12:03 Swift Tutorial - Swift Protocols
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Protocols
Introduction « Previous (0410__Swift_Extensions.htm) Next » (0430__Swift_Generics.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
Protocols is like the interfaces in Java and C#.
(0040__Swift_String.htm)
Swift Booleans The protocols defines the properties, methods, and types that would require other types to
(0050__Swift_Booleans.htm)
implement.
Swift Tuples
(0060__Swift_Tuples.htm) A protocol can be adopted or implemented by a type.
Swift Optionals
(0070__Swift_Optionals.htm) Adopting a protocol means that a type will implement the definition defined in the protocol.
Swift Global/Local Variables
To define a protocol, use the protocol keyword.
(0080__Swift_Global_Local_Variables.htm)
protocol Printer {
Operator func print()
Swift Assertions }
(0090__Swift_Assertions.htm)
Swift Assignment Operators The code above created a new protocol named Printer , which defined a function named
(0100__Swift_Assignment_Operators.htm)
print() .
Swift Arithmetic Operators
(0110__Swift_Arithmetic_Operators.htm)If you coded a class that you wanted to adopt this protocol, you would write the code as follows.
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)class aClass:Printer {
Swift Range Operators
(0130__Swift_Range_Operators.htm) }
Swift Logical Operators
(0140__Swift_Logical_Operators.htm) In the code above, we defined a class that adopted the Printer protocol.
Advanced Data Type When you adopt a protocol, the protocol name goes after the class name and a colon : .
Swift Enumerations
(0150__Swift_Enumerations.htm)
Swift Arrays
(0160__Swift_Arrays.htm)
Swift Dictionaries
(0170__Swift_Dictionaries.htm)
Example
Statement If the class is also inheriting another class, you must list the protocol name after the parent class.
Swift Comments
(0190__Swift_Comments.htm) For instance, if aClass inherited the Person class, you would need to use the code as follows.
Swift for loop
(0200__Swift_for.htm) protocol Printer {
Swift while loop func print()
(0210__Swift_while.htm) }
Swift if (0220__Swift_if.htm)
class aClass:Printer {
Swift switch
func print() {
(0230__Swift_switch.htm)
println("Implement print for aClass")
Swift continue break
}
(0240__Swift_continue-break.htm) }
Swift Closures
(0310__Swift_Closures.htm) When you use protocols to specify variables, you must include the getter and/or setter
definitions.
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0420__Swift_Protocols.htm 1/2
13/02/2025, 12:03 Swift Tutorial - Swift Protocols
Swift Class
protocol Printer {
(0330__Swift_Class.htm) func print()
Swift Properties var i:Int {get set}
(0340__Swift_Properties.htm) }
Swift Methods
(0350__Swift_Methods.htm) class aClass:Person, Printer {
Swift Subscripts
(0360__Swift_Subscript.htm) var i = 0
Swift Inheritance
(0370__Swift_Inheritance.htm) func print() {
Swift Initialization println("Implement print for aClass & i = \(self.i)")
}
(0380__Swift_Initialization.htm)
}
Swift Type Casting
(0390__Swift_Type_Casting.htm)
var obj = aClass()
Swift Nested Types
(0400__Swift_Nested_Types.htm) obj.print()
Swift Extensions
(0410__Swift_Extensions.htm)
Swift Protocols ()
Swift Generics
(0430__Swift_Generics.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0420__Swift_Protocols.htm 2/2
13/02/2025, 12:03 Swift Tutorial - Swift Generics
Search
Website Home
HOME (http://www.java2s.com)
Swift Tutorial - Swift Generics
Introduction « Previous (0420__Swift_Protocols.htm)
Swift Introduction (index.htm)
Swift Constants/Variables
(0010__Swift_Constants_Variables.htm)
Data Type
Swift Data Types
(0020__Swift_Data_Types.htm)
Swift Number
(0030__Swift_Number.htm)
Swift String
Generics means parameterized types. Using generics, we can create a single class that works
(0040__Swift_String.htm)
Swift Booleans with different types of data. A class, protocal, or method that operates on a parameterized type is
(0050__Swift_Booleans.htm) called generic.
Swift Tuples
(0060__Swift_Tuples.htm) To use generics, you must add <T> after a function name.
Swift Optionals
Then you can substitute T in place of the type name.
(0070__Swift_Optionals.htm)
Swift Global/Local Variables
(0080__Swift_Global_Local_Variables.htm)Example
The following code shows how to define function with generic parameters.
Operator
Swift Assertions
Type Checking
(0090__Swift_Assertions.htm) class Person {
Swift Assignment Operators func profile() -> String {
(0100__Swift_Assignment_Operators.htm) return "Person"
Swift Arithmetic Operators }
(0110__Swift_Arithmetic_Operators.htm) }
Swift Comparison Operators
(0120__Swift_Comparison_Operators.htm)class Employee: Person {
Swift Range Operators override func profile() -> String {
(0130__Swift_Range_Operators.htm) return "Employee"
Swift Logical Operators }
}
(0140__Swift_Logical_Operators.htm)
var p1 = Person()
var e1 = Employee()
Advanced Data Type
Swift Enumerations func printPerson<T>(p:T){
(0150__Swift_Enumerations.htm) let o = p as Person
Swift Arrays println(o.profile())
(0160__Swift_Arrays.htm) }
Swift Dictionaries
printPerson (p1)
(0170__Swift_Dictionaries.htm)
printPerson (e1)
Statement
Swift Comments
(0190__Swift_Comments.htm)
Swift for loop
(0200__Swift_for.htm)
Swift while loop
(0210__Swift_while.htm)
Swift if (0220__Swift_if.htm)
Swift switch Example 2
(0230__Swift_switch.htm)
The following code shows how you would add type checking to your function.
Swift continue break
(0240__Swift_continue-break.htm)
Function
Swift Functions
(0300__Swift_Functions.htm)
Swift Closures
(0310__Swift_Closures.htm)
Class/Structure
Swift Structures
(0320__Swift_Structures.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0430__Swift_Generics.htm 1/2
13/02/2025, 12:03 Swift Tutorial - Swift Generics
Swift Class
class Person {
(0330__Swift_Class.htm) func profile() -> String {
Swift Properties return "Person"
(0340__Swift_Properties.htm) }
Swift Methods }
(0350__Swift_Methods.htm)
Swift Subscripts class Employee: Person {
(0360__Swift_Subscript.htm) override func profile() -> String {
Swift Inheritance return "Employee"
(0370__Swift_Inheritance.htm) }
Swift Initialization }
(0380__Swift_Initialization.htm)
func printPerson<T>(p:T){
Swift Type Casting
if p is Person || p is Employee{
(0390__Swift_Type_Casting.htm)
let o = p as Person
Swift Nested Types
println(o.profile())
(0400__Swift_Nested_Types.htm) }
Swift Extensions else{
(0410__Swift_Extensions.htm) println("\(p) is not supported.")
Swift Protocols }
(0420__Swift_Protocols.htm) }
Swift Generics ()
var p1 = Person()
var e1 = Employee()
printPerson (p1)
printPerson (e1)
var s = "ABC"
printPerson(s)
« Previous (0420__Swift_Protocols.htm)
www.java2s.com/Tutorials/Swift/Swift_Language/0430__Swift_Generics.htm 2/2