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

Type Casting

Type casting in Swift is implemented with is and as operators. Type casting: A subclass instance can be use as a superclass instance. Defining a Class Hierarchy class Media { var name: String init(name: String) { self.name = name } } class Song: Media { var artist: String init(name: String, artist: String) { self.artist = artist super.init(name: name) } } class Movie: Media { var director: String init(name: String, director: String) { self....

May 16, 2022

Concurrency

Swift has built-in support for writing asynchronous and parallel code in a structured way. Parallel code means multiple pieces of code run simultaneously. Concurrency will make the code harder to debug. Swift can help to catch problem at compile time. Although it’s possible to write concurrent code without using Swift language support, that code tends to be hard to read. listPhotos(inGallery: "Summer Vacation") { photoNames in let sortedNames = photoNames.sorted() let name = sortedNames[0] downloadPhoto(named: name) { photo in show(photo) } } Keywords:...

May 8, 2022