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

Error Handling

A class support for throwing, catching, propagating, and manipulating recoverable errors at runtime. When an optional fails, it’s useful to understand what cause the failure, so that the code can respond accordingly. Example: Reading a file from the disk may be fail in some way. File not exist Have no permission to read. File not being encoded in a compatible format. Distinguishing among these different situations allows a program to resolve some errors and to communicate to the user any errors it can’t resolve....

May 7, 2022

Deinitialization

Before the object is recycle, the deinitializer solve the post events.

May 2, 2022

Initialization

The class’s initializer set the original state for the object.

April 30, 2022

Inheritance

A class inherit another class call super class, and get some ability from super class.

April 24, 2022

Method

The object have some method to provide functionality.

April 17, 2022

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....

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....

April 10, 2022