This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Init, Deinit | |
class User { | |
var name: String | |
var age: Int | |
var address: String | |
var emailID: String | |
// Failable + Designated Init |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Keypath , WritableKeyPath, ReferenceWritableKeyPath | |
struct StarShip { | |
var name: String | |
var maxWrap: Double | |
} | |
var voyager = StarShip(name: "Voyager", maxWrap: 9.97) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// rethrowing function and method | |
// https://stackoverflow.com/questions/49672847/how-to-declare-a-rethrowing-function | |
/* A function or method can be declared with the rethrows keyword to indicate that it throws an error only | |
if one of its function parameters throws an error. These functions and methods are known as rethrowing functions and | |
rethrowing methods. Rethrowing functions and methods must have at least one throwing function parameter. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Result type is enum with two cases, success and error. | |
// Introduced in swift 5 | |
enum ScoreboardError: Error { | |
case serverDown | |
case badURL | |
case unknown | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Mutating function | |
// The properties of value types cannot be modified within its instance methods by default. | |
struct User { | |
var name : String | |
var age : Int | |
var address : String | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Factory Design pattern | |
struct Person { | |
var name : String | |
} | |
class PersonFactory { | |
func createPerson() -> Person? { | |
nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Public vs Open | |
// properties and function declared using `public` or `open` access specifier is accessible outside module | |
// but in case of inheritance `public` swift compiler will not allow you to override it. | |
// Private vs FilePrivate | |
// Properties and Methods declared using `private` access specifier is accessible within declared type. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Opaque types | |
/* | |
A function or method with an opaque return type hides its return value’s type information. | |
Instead of providing a concrete type as the function’s return type, the return value is | |
described in terms of the protocols it supports. Hiding type information is useful at | |
boundaries between a module and code that calls into the module, because the underlying type of | |
the return value can remain private. Unlike returning a value whose type is a protocol type, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Property Observers - `willSet`, `didSet` | |
// Property observers let you observe the change in the state or value | |
// of properties and provides single source of truth | |
// 'didSet' and 'willSet' cannot be provided together with a getter | |
// If `set` is provided then it is mandotory to add `get` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Property Wrapper | |
// A property wrapper adds a layer of separation between code that manages how a property is stored and | |
// the code that defines a property. For example, if you have properties that provide thread-safety checks or store | |
// their underlying data in a database, you have to write that code on every property. When you use a property wrapper, | |
// you write the management code once when you define the wrapper, and then reuse that management code by applying it to multiple properties. | |
@propertyWrapper |