This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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]; | |
} | |
}); |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
protocol RowPresentable { | |
var string: String { get } | |
// it conforms to both protocol + class | |
var rowVC: UIViewController & PanModalPresentable { get } | |
} |
This file contains hidden or 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
// 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) |
This file contains hidden or 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
// 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) |
This file contains hidden or 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
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 } |
This file contains hidden or 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
// 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]; |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
// 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 |