Skip to content

Instantly share code, notes, and snippets.

View JarvisTheAvenger's full-sized avatar
🎯
Focusing

Rahul Umap JarvisTheAvenger

🎯
Focusing
View GitHub Profile
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.
import Alamofire
import SwiftyJSON
class APIManager {
private init(){}
class func request(
_ url: URLConvertible,
method: HTTPMethod,
var isDataLoading = false
var orders = []
var startIndex = 0
var activityIndicator = UIActivityIndicatorView()
func loadMoreOrders() {
//Get order from server handle your UI accordingly.
}
import Foundation
let json = """
[
{
"name": {
"firstName": "Taylor",
"lastName": "Swift"
},
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.
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 ===
import Foundation
// Class vs Static
class A {
class func doSomething() {
print("do something")
}
// Static functions can't be overridden
static func createSomething() {
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) {
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
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 {