Advanced Operators

Unlike arithmetic operators in C, arithmetic operators in Swift don’t overflow by default. If want to overflow by default, use the overflow operation begin with ampersand (&). For example, the overflow addition operator (&+). It’s so free to define custom infix, prefix, postfix and assignment operators, precedence and associativity values. Bitwise Operators Here we use a function to pad 0 for the number’s print result. func pad(num: UInt8, count: Int) -> String { var str = String(num, radix: 2) var res: String = str for _ in 0....

June 19, 2022

Access Control

Access control restrict access to part of the code form code in other source files and modules. It enable us to hide the implement detail of the code, and to specify a preferred interface which that code can be accessed and used. Set access levels to individual types (classes, structures, enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types. Protocols can be restricted to a certain context, as can global constants, variable, and functions....

June 18, 2022

Memory Safty

Most of the time we don’t have to think about accessing memory, but it’s important to understand where potential conflicts can occur, so we can avoid writing code that has conflicting access to memory. Here we are talking about the situation that happen on a single thread. Memory Access var one = 1 // write access to the memory one is stored. print("\(one)") // read access from the memory one is stored....

June 12, 2022

Automatic Reference Counting

Swift use ARC to track and manage the app’s memory usage. ARC frees up the memory used by class instances when those instances are no longer needed. Reference counting applies only to instance of classes. Reference: strong: retain the obj. (default) weak: don’t retain the object referred to, track the object referred to. unowned: don’t retain the object referred to, don’t tract the object referred to. How ARC Work Allocate a chunk of memory to store information about that instance...

June 12, 2022

Opaque Type

A function with an opaque type hides its return value’s type information. Hiding type information at some boundaries between a module and code that calls into the module. Unlike returning a value whose type is a protocol type, opaque type preserve type identity —the compile has access to the type information, but clients of the module don’t. The Situation Here we have a Shape protocol. protocol Shape { func draw() -> String } The struct Triangle conform to the Shape....

June 5, 2022

Generics

Define a class with template type , then should illustract the type when use the class.

June 5, 2022

Optional Chaining

A process for querying and calling properties, methods, and subscripts on an optional that might currently be nil .Multiple queries can be chained together, and the chain fails gracefully if any link in the chain is nil . Optional Chaining as an Alternative to Forced Unwrapping ! force unwrapping triggers a runtime error when the optional is nil . Use the optional chaining to check if the optional value querying is succeed....

May 24, 2022

Protocol

A protocol defines a blueprint with methods, properties.

May 22, 2022

Extension

Extension add new functionality to an existing class, structure, enumeration, or protocol type.

May 21, 2022

Nested Types

Enumerations, classes or structures can be nested in another type. struct Closh { enum Size: String{ case H = "high", M = "Medium", L = "Low" } enum Detail: Int { case H = 180, M = 170, L = 160 struct Price { let normal: Int, discount: Int? } var price: Price { switch self { case .H: return Price(normal: 100, discount: 90) case .M: return Price(normal: 90, discount: 80) case ....

May 16, 2022