Skip to content

Instantly share code, notes, and snippets.

View aainaj's full-sized avatar

Aaina Jain aainaj

  • GoJek Tech
  • Bangalore, India
View GitHub Profile
@aainaj
aainaj / ProductClass.swift
Last active May 28, 2018 02:51
Reference Type
import Foundation
class Product: NSObject {
let identifier: Int
init(identifier: Int) {
self.identifier = identifier
super.init()
}
}
@aainaj
aainaj / assert.swift
Last active June 18, 2018 05:50
Assert function
/* syntax:
public func assert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = default,
file: StaticString = #file,
line: UInt = #line)
*/
let minAge = 2
assert(minAge >= 3, "Age did not meet expectation")
@aainaj
aainaj / assertionFailure.swift
Last active June 18, 2018 05:51
AsserstionFailure
/* syntax:
public func assertionFailure(
_ message: @autoclosure () -> String = default,
file: StaticString = #file,
line: UInt = #line)
*/
guard age >= 3 else {
return assertionFailure("Age can't be less than 3")
}
@aainaj
aainaj / precondition.swift
Created June 18, 2018 05:53
Precondition
/* syntax:
public func precondition(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String = default,
file: StaticString = #file,
line: UInt = #line)
*/
let minAge = 2
precondition(minAge >= 3, "Age did not meet expectation")
@aainaj
aainaj / preconditionFailure.swift
Last active June 19, 2018 02:34
PreconditionFailure
/* syntax:
public func preconditionFailure(
_ message: @autoclosure () -> String = default,
file: StaticString = #file,
line: UInt = #line) -> Never
*/
func validateAge(age: Int) -> Bool {
guard age >= 3 else {
preconditionFailure("Age can't be less than 3")
}
@aainaj
aainaj / fatalError.swift
Created June 19, 2018 02:34
fatalError
/* syntax:
public func fatalError(
_ message: @autoclosure () -> String = default,
file: StaticString = #file,
line: UInt = #line) -> Never
*/
func validateAge(age: Int) -> Bool {
guard age >= 3 else {
fatalError("Age can't be less than 3")
}
@aainaj
aainaj / Closures.swift
Last active June 27, 2018 02:21
Closures Examples
//: Playground - Closures
// Closure take no parameter and return nothing
let sayHello: () -> Void = {
print("Hello")
}
sayHello()
// Closure take one parameter and return 1 parameter
@aainaj
aainaj / TrailingClosure.swift
Created June 27, 2018 03:17
Trailing closure for reduce method
let digitsList = [1, 2, 3, 4, 5]
let sum = digitsList.reduce(0) { $0 + $1 }
print(sum)
// prints 15
@aainaj
aainaj / CaptureList.swift
Created June 27, 2018 03:32
Closure CaptureList
//: Playground - Closures
import Foundation
class CaptureList: NSObject {
let digit = 5
override init() {
super.init()
makeSquareOfValue { [digit] squareDigit in
@aainaj
aainaj / ClosureWithTypeAlias.swift
Created June 27, 2018 03:40
Closures with type alias
//: Playground - Closures
import Foundation
class CaptureList: NSObject {
let digit = 5
typealias onCompletionHandler = (Int) -> Void
override init() {
super.init()