Skip to content

Instantly share code, notes, and snippets.

View natanrolnik's full-sized avatar

Natan Rolnik natanrolnik

View GitHub Profile
source 'https://rubygems.org'
gem 'cocoapods', '~> 1.5.3'
gem 'fastlane', '~> 2.100.1'
@natanrolnik
natanrolnik / guardweak.codesnippet
Last active October 26, 2018 09:54
Guard weak self Code Snippet
<!--
Related: https://twitter.com/natanrolnik/status/1055756310023716865
The following snippet should be copied to:
~/Library/Developer/Xcode/UserData/CodeSnippets
as, for example, guardweak.codesnippet.
When you type `guardweakself` inside a function scope, it will generate the following code snippet:
guard let <#self#> = <#self#> else {
@natanrolnik
natanrolnik / IfAndSwitchAsExpression.swift
Created February 7, 2019 11:09
if and switch as expressions
import Foundation
let randomInt = Int.random(in: 0...3)
let spelledOut: String = {
switch randomInt {
case 0:
return "Zero"
case 1:
return "One"
@natanrolnik
natanrolnik / Scheduler2.swift
Created February 8, 2019 14:06
Another small wrapper for multiple DispatchWorkItems
PlaygroundPage.current.needsIndefiniteExecution = true
typealias DispatcherIdentifier = String
extension DispatchQueue {
static var associatedValueKey = 0
func schedule(after timeInterval: TimeInterval,
with identifier: DispatcherIdentifier,
action: @escaping () -> Void) {
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
self?.micButton.jump()
}
//keep a variable to know if the user tapped the button:
var micButtonTapped = false
func recordVoiceMessage() {
//if the user tapped/held the mic button, set the variable to true
micButtonTapped = true
}
//in your viewDidAppear method, schedule it:
DispatchQueue.main.asyncAfter(.now() + 3) { [weak self] in
var micHintWorkItem: DispatchWorkItem?
func recordVoiceMessage() {
micHintWorkItem?.cancel()
}
//in your viewDidAppear method, create and schedule the work item:
micHintWorkItem = DispatchWorkItem { [weak self] in
self?.micButton.jump()
}
@natanrolnik
natanrolnik / CoreData+Extensions.swift
Last active May 14, 2019 12:50
Core Data Helpers
//The following code depends on this:
//https://gist.github.com/sisoje/f1444dff45618938ce81324a81316690#file-nspredicate-keypath-operators-swift
import CoreData
extension NSManagedObject {
static var entityName: String {
return entity().name!
}
}
//declare which keys in the JSON we are interested in
enum CodingKeys: String, CodingKey {
case status
case confirmedUsers
case position
case reason
}
//declare the possible values os the status key
private enum EventConfirmationStatus: String, Codable {
@natanrolnik
natanrolnik / Redundant Else
Created December 18, 2019 14:08
Examples of redundant else usage
------ ⭣ Redundant Else ⭣ ------
if someCondition {
return meh
} else {
let someValue = callSomeFunction()
return someValue
}
----------- ⭣ Fine ⭣ -----------