This file contains 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 | |
protocol Fooable { | |
func doFoo() | |
} | |
struct Fooer : Fooable { | |
func doFoo() { | |
// Do the real thing here. This might modify a DB, require an internet connection, | |
// do a long-running computation, etc. |
This file contains 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 Alamofire | |
import SwiftyJSON | |
class APIManager { | |
private init(){} | |
class func request( | |
_ url: URLConvertible, | |
method: HTTPMethod, |
This file contains 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
var isDataLoading = false | |
var orders = [] | |
var startIndex = 0 | |
var activityIndicator = UIActivityIndicatorView() | |
func loadMoreOrders() { | |
//Get order from server handle your UI accordingly. | |
} |
This file contains 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 | |
let json = """ | |
[ | |
{ | |
"name": { | |
"firstName": "Taylor", | |
"lastName": "Swift" | |
}, |
This file contains 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 | |
// Async - Await - requires Xcode 13 and later | |
// yielding the thread | |
/* | |
Swift suspends the execution of your code on the current thread and runs some other code on that thread instead. | |
Because code with await needs to be able to suspend execution, only certain places in your program can call asynchronous functions or methods: | |
1. Code in the body of an asynchronous function, method, or property. | |
2. Code in the static main() method of a structure, class, or enumeration that’s marked with @main. |
This file contains 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 | |
// Class vs Struct | |
// reference vs value type | |
// Class supports inheritance, struct don't support inheritance | |
// Class supports ARC, in struct you don't have to worry about memory. | |
// Structs are thread safe | |
// Only class provides objective c interoperability | |
// Class provides built in identity operator === |
This file contains 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 | |
// Class vs Static | |
class A { | |
class func doSomething() { | |
print("do something") | |
} | |
// Static functions can't be overridden | |
static func createSomething() { |
This file contains 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 | |
// Closures | |
// Escaping closures vs Non Escaping closures | |
// Escaping closures escapes the function body | |
func doSomething(_ a: Int, completion: @escaping (Int) -> Void) { | |
var a = a | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { |
This file contains 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 | |
// Error is protocol | |
// NSError is class | |
// NSError(domain: <#T##String#>, code: <#T##Int#>, userInfo: <#T##[String : Any]?#>) | |
enum BoardError : Error { | |
case failed | |
case resultPending | |
case unknown |
This file contains 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 | |
// Generics | |
/* Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner. | |
*/ | |
// Normal function | |
func add(first: Int, second: Int) -> Int { |
OlderNewer