Properties

Stored properties Only provided by classed and structures. Computed properties Provide by classes, structures, enumerations. Usually associated with instance of a particular type, can be associated with the type itself(type properties). Can define property observers to monitor changes in a property’s value, which can respond to with custom action. Can use a property wrapper to reuse code in the getter and setter. Stored Properties **struct Setting { var width: Int let height: Int } var appSetting = Setting(width: 100, height: 200) appSetting.width = 150 let constSetting = Setting(width: 110, height: 210) //constSetting.width = 120 // This will trigger a compile-time error, the struct is of const value type.** Lazy Stored Properties Its initial value isn’t calculated until the first time it’s used. ...

April 17, 2022

Structures and Classes

The object oriented programming.

April 10, 2022

Enumerations

Syntax enum Direction { case up case down case left case right } Multiple case can appear on a single line, separated by commas: enum { case up, down, left, right } Use the Enumeration var dir = Direction.up When we want to modify the var after the initialized, we can use a shorter form of the enumeration. var dir = Direction.up dir = .down // The value's type has been inferred when the value is in initializing. Matching Enumeration Values with a Switch Statement enum Direction { case up case down case left case right } var dir = Direction.right switch dir { case .up: print("Go up") case .down: print("Go down") case .left: print("Go left") case .right: print("Go right") } Iterating over Enumeration Cases Conform to the CaseIterable protocol, to make the enumeration’s case be iterable. ...

April 10, 2022

Subscripts

Set subscript(index: Int) in the class to index the data of the instance of the this class.

April 10, 2022

Closures

The closures can cantains something by the curly bracket.

April 2, 2022

Function

Call the function(method) to do something detail.

April 2, 2022

Control Flow

loop, condition branch, control transfer.

March 28, 2022

Collection Type

The array, set, and dictionary.

March 28, 2022

Strings and Characters

String type is bridged with Foundation’s NSString. Foundation extends String to expose methods defines by NSString. If import Foundation, you can access those NSString methods on String without casting. String Literals // "Hello World!" is a String literal. let sentence = "Hello World!" // Multiline let story = """ There are some people in the room. They are having a party. Because today is the Christmas. """ // every line has the line breaks. // the start and end sign(""") must take a single line. // backlash(\), it means that the string is not broken. Then line breaks not to be part of the string's value. let content = """ In a happy atmos\ phere. We start the conversation """ print(content) // result: In a happy atmosphere. We start the conversation. // Alignment of quotation marks in the multiline string. let desc = """ Ha it's so funny. """ print(desc) // result: Ha its so funny. let desc = """ Ha it's so funny. """ print(desc) // result: Ha its so funny. let desc = """ Ha it's so funny. """ print(desc) // result: Ha its so funny. Special Character in String Literals ...

March 20, 2022

Basic Operators

Basic usage of the operators.

March 19, 2022