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 / Device.swift
Created January 3, 2018 18:05
Swift equivalent code of #define
struct Device {
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_RETINA = UIScreen.main.scale >= 2.0
static let SCREEN_WIDTH = Int(UIScreen.main.bounds.size.width)
static let SCREEN_HEIGHT = Int(UIScreen.main.bounds.size.height)
static let SCREEN_MAX_LENGTH = Int( max(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let SCREEN_MIN_LENGTH = Int( min(SCREEN_WIDTH, SCREEN_HEIGHT) )
@aainaj
aainaj / SafeAreaLayout.swift
Last active January 25, 2018 03:46
Safe area layout
class ViewController: UIViewController {
let wrapperView = UIView()
let cyanView = UIView()
override func viewDidLoad() {
view.backgroundColor = .yellow
view.addSubview(wrapperView)
wrapperView.addSubview(cyanView)
@aainaj
aainaj / AdditionalSafeAreaInsets.swift
Last active January 30, 2018 16:39
Additional Safe Area Insets
override var additionalSafeAreaInsets: UIEdgeInsets {
set {
super.additionalSafeAreaInsets = UIEdgeInsetsMake(10.0, 50.0, 10.0, 50.0)
}
get {
return UIEdgeInsetsMake(10.0, 50.0, 10.0, 50.0)
}
}
@aainaj
aainaj / SafeAreaInsets.swift
Created January 30, 2018 16:39
Safe Area Insets
import UIKit
class ViewController: UIViewController {
let wrapperView = UIView()
let cyanView = UIView()
override func viewDidLoad() {
view.backgroundColor = .green
view.addSubview(wrapperView)
@aainaj
aainaj / simrecord
Created March 20, 2018 03:14 — forked from JohnSundell/simrecord
🎥 Script that lets you start a video recording from the iOS simulator with one command
#!/bin/bash
ITERATION=1
EXTENSION="mp4"
FILENAME="$HOME/Desktop/Simulator Recording.$EXTENSION"
while [ -e "$FILENAME" ]
do
ITERATION=$((ITERATION+1))
FILENAME="$HOME/Desktop/Simulator Recording $ITERATION.$EXTENSION"
@aainaj
aainaj / rx.share.swift
Last active May 5, 2018 15:13
RxSwift share operator
let observable = Observable.from([1, 2, 3]).share()
let disposeBag = DisposeBag()
observable.subscribe(onNext: { element in
print("⛴ \(element)")
}).addDisposableTo(disposeBag)
observable.subscribe(onNext: { element in
print("✈️ \(element)")
}).addDisposableTo(disposeBag)
@aainaj
aainaj / struct.swift
Last active May 20, 2018 03:16
Creates simple struct and print memory address
import Foundation
class ProductFactory {
var items = 8
}
struct Product {
let identifier: Int
let name: String
let inStock: Bool
@aainaj
aainaj / OptionalPropertyStruct.swift
Last active May 20, 2018 15:44
Optional property types for struct
import Foundation
struct Product {
let identifier: Int
let name: String
var description: String?
init(identifier: Int, name: String) {
self.identifier = identifier
self.name = name
@aainaj
aainaj / MutableReferenceTypeInStruct.swift
Last active July 13, 2018 04:50
Mutable Reference Type in Struct
import Foundation
class ProductFactory: NSObject, NSCopying {
var items = 8
func copy(with zone: NSZone? = nil) -> Any {
let factory = ProductFactory()
factory.items = items
return factory
}
@aainaj
aainaj / Stack.md
Created May 23, 2018 17:25 — forked from cpq/Stack.md
Why stack grows down

Why stack grows down

Any running process has several memory regions: code, read-only data, read-write data, et cetera. Some regions, such as code and read-only data, are static and do not change over time. Other regions are dynamic: they can expand and shrink. Usually there are two such regions: dynamic read-write data region, called heap, and a region called stack. Heap holds dynamic memory allocations, and stack is mostly used for keeping function frames.

Both stack and heap can grow. An OS doesn't know in advance whether stack or heap will be used predominantly. Therefore, an OS must layout these two memory regions in a way to guarantee maximum space for both. And here is the solution:

  1. Layout static memory regions at the edges of process's virtual memory
  2. Put heap and stack on edges too, and let them grow towards each other: one grows up, one grows down