Skip to content

Instantly share code, notes, and snippets.

View romyilano's full-sized avatar
😎
improving

Romy romyilano

😎
improving
View GitHub Profile
// brushing up on url components
//
class CryptoApi {
static let host = "min-api.cryptocompare.com"
static let allCoinsPath = "/data/all/coinlist"
var allCoinsUrl: URL? = {
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = CryptoApi.host
// https://stackoverflow.com/questions/23806751/strong-reference-to-a-weak-references-inside-blocks
__weak typeof(self) weakSelf = self;
void (^someBlock)(id) = ^(id data){
if (weakSelf != nil) {
// last remaining strong reference released by another thread.
// weakSelf is now set to nil.
[myArray addObject:weakSelf];
}
});
@romyilano
romyilano / Retain.m
Last active April 7, 2019 21:34
retain cycle sin blocks in objective-c thru apple. //
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW16
@interface XYZBlockKeeper : NSObject
@property (copy) void (^block)(void);
@end
@implementation XYZBlockKeeper
- (void)configureBlock {
self.block = ^{
[self doSomething]; // capturing a strong reference to self
@romyilano
romyilano / DoubleProtocol.swift
Created March 25, 2019 15:59
From the PanModal GitHub... interesting!
protocol RowPresentable {
var string: String { get }
// it conforms to both protocol + class
var rowVC: UIViewController & PanModalPresentable { get }
}
@romyilano
romyilano / ParentViewController.swift
Created March 23, 2019 23:55
Handling container views in code
// thanks dude
// https://medium.com/@dushyant_db/setting-up-a-container-view-using-interface-builder-and-via-code-7ac1a7f0a0d6
override func viewDidLoad() {
super.viewDidLoad()
guard let childVC = self.storyboard?.instantiateViewController(withIdentifier: "ChildViewController") as? ChildViewController else {
return
}
addChildViewController(childVC)
@romyilano
romyilano / ParentViewController.swift
Created March 23, 2019 23:55
Handling container views in code
// thanks dude
// https://medium.com/@dushyant_db/setting-up-a-container-view-using-interface-builder-and-via-code-7ac1a7f0a0d6
override func viewDidLoad() {
super.viewDidLoad()
guard let childVC = self.storyboard?.instantiateViewController(withIdentifier: "ChildViewController") as? ChildViewController else {
return
}
addChildViewController(childVC)
@romyilano
romyilano / ListNode.swift
Last active April 1, 2019 15:35
ListNode helpers for LeetCode
public class ListNode {
public var val: Int
public var next: ListNode?
public init(_ val: Int) {
self.val = val
self.next = nil
}
convenience init?(withArray array: [Int]) {
guard let firstVal = array.first else { return nil }
@romyilano
romyilano / AutoreleasePoolSample.m
Created February 18, 2019 16:53
This is SO useful!
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html
– (id)findMatchingObject:(id)anObject {
id match;
while (match == nil) {
@autoreleasepool {
/* Do a search that creates a lot of temporary objects. */
match = [self expensiveSearchForObject:anObject];
func hasCycle(head: ListNode) -> Bool {
while let hareNext = hare.next?.next, let tortoiseNext = tortoise.next {
hare = hareNext
tortoise = tortoiseNext
if hare.val == tortoise.val {
return true
}
}
return false
}
// assuming nodes have no duplicate values
var head = ListNode(5)
head.next = ListNode(7)
let secondNode = head.next
head.next?.next = ListNode(8)
head.next?.next?.next = ListNode(12)
head.next?.next?.next?.next = secondNode
var tortoise = head
var hare = head