Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
sundeepgupta / singleton.swift
Last active March 30, 2017 17:14
Swift singleton with initialization injection
class CoreGlobal {
fileprivate static let shared = CoreGlobal()
fileprivate let device: DeviceManager
private class Config {
var deviceManager: DeviceManager?
}
private static let config = Config()
@discardableResult
@sundeepgupta
sundeepgupta / animated-ellipsis.swift
Created March 2, 2017 19:35
Swift Animating Ellipsis Loader
class LoadingLabel: UILabel {
var timer: Timer?
let states = [".", "..", "..."]
var currentState = 0
func start() {
stop(withText: "")
timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(update), userInfo: nil, repeats: true)
timer?.fire()
@sundeepgupta
sundeepgupta / thin-frameworks.sh
Created February 23, 2017 13:58
Bash script to thin fat iOS frameworks. Strips non-valid architectures from fat framework binaries.
# Adapted from https://github.com/realm/realm-cocoa/blob/master/scripts/strip-frameworks.sh
# This script strips all non-valid architectures from dynamic libraries in
# the application's `Frameworks` directory which is required for App Store submission.
#
# The following environment variables are required:
#
# BUILT_PRODUCTS_DIR
# FRAMEWORKS_FOLDER_PATH
# VALID_ARCHS
@sundeepgupta
sundeepgupta / create-fat-framework.sh
Last active September 19, 2023 08:11
Script to create a universal or "fat" binary for an iOS framework.
#!/bin/bash
# Adapted from http://stackoverflow.com/questions/24039470/xcode-6-ios-creating-a-cocoa-touch-framework-architectures-issues/26691080#26691080
# and https://gist.github.com/cromandini/1a9c4aeab27ca84f5d79
# Create a new aggregate target.
# For the automatically generated scheme, change its build config to "release".
# Ensure this target's "product name" build setting matches the framework's.
# Add a run script with `source "${PROJECT_DIR}/path_to_this_script`
@sundeepgupta
sundeepgupta / swift-swizzling.swift
Created February 13, 2017 18:00
Swift swizzling
extension TheClass {
class func swizzle(_ original: Selector, with swizzled: Selector, for klass: AnyClass) {
let originalMethod = class_getInstanceMethod(klass, original)
let swizzledMethod = class_getInstanceMethod(klass, swizzled)
method_exchangeImplementations(originalMethod, swizzledMethod)
}
func swizzle(_ original: Selector, with swizzled: Selector) {
let klass: AnyClass! = object_getClass(self)
@sundeepgupta
sundeepgupta / TypeErasureExample.swift
Created February 6, 2017 01:02
Swift Type Erasure Example
import UIKit
protocol Decodable {
func decode() -> Void
}
struct Action {
let decodable: AnyDecodable
}
@sundeepgupta
sundeepgupta / http.js
Created August 11, 2016 14:27
node.js http module
'use strict';
const HTTPS = require('https');
const URL = require('url');
const QueryString = require('querystring');
module.exports = function() {
return {
postJSON: function(url, params) {
const data = JSON.stringify(params);
function containsQuestion(message) {
return 1;
}
function containsYou(message) {
return 0;
}
function senderRelationshipType(message) {
return 2;
@sundeepgupta
sundeepgupta / jobextension.swift
Last active April 25, 2016 01:09
Job extension
func loadJobs() {
let sharedDefaults = NSUserDefaults(suiteName: "group.ca.sundeepgupta.job")
if let jobs = sharedDefaults?.arrayForKey("jobs") as? [[String: String]] {
self.jobs = jobs
} else {
sharedDefaults?.setObject([], forKey: "jobs")
}
self.tableView.reloadData()
}
@sundeepgupta
sundeepgupta / CloudKitSnippets.swift
Last active April 11, 2016 16:20
CloudKit Snippets
// Create Record
let record = CKRecord(recordType: "Posting")
record["url"] = self.urlField.text!
record["name"] = self.nameField.text!
// Get Database
let container = CKContainer.defaultContainer()
let database = container.publicCloudDatabase