Skip to content

Instantly share code, notes, and snippets.

View db42's full-sized avatar

Dushyant Bansal db42

View GitHub Profile
@db42
db42 / SelfSizedTableView.swift
Created March 2, 2018 02:24
Swift 4 recipe: Self sizing table view
//
// SelfSizedTableView.swift
// AdjustableTableView
//
// Created by Dushyant Bansal on 25/02/18.
// Copyright © 2018 db42.in. All rights reserved.
//
import UIKit
@db42
db42 / NotificationBanner.swift
Created February 11, 2018 12:16
Swift4 recipe: How to show top notification banner without using 3rd party library
//
// NotificationBanner.swift
// TopNotificationRecipe
//
// Created by Dushyant Bansal on 11/02/18.
// Copyright © 2018 db42.in. All rights reserved.
//
import Foundation
import UIKit
@db42
db42 / ruby-upload-s3.rb
Created February 9, 2018 12:19
Ruby script to upload a file to amazon s3
require 'aws-sdk-s3' # v2: require 'aws-sdk'
ACCESS_KEY_ID = ""
SECRET_ACCESS_KEY = ""
REGION_ID = "us-east-1"
BUCKET_NAME = "bucket-name"
def uploadS3
credentials = Aws::Credentials.new(
ACCESS_KEY_ID,
@db42
db42 / share-screenshot.swift
Created December 25, 2017 03:26
Swift 4 recipes: How to present share options when user takes a screenshot
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(handleScreenshot), name: Notification.Name.UIApplicationUserDidTakeScreenshot, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIApplicationUserDidTakeScreenshot, object: nil)
}
@db42
db42 / swift4-plist-extract.swift
Last active February 2, 2019 14:20
Swift 4 recipes: Extract data from a property list
//Property List file name = regions.plist
let pListFileURL = Bundle.main.url(forResource: "regions", withExtension: "plist", subdirectory: "")
if let pListPath = pListFileURL?.path,
let pListData = FileManager.default.contents(atPath: pListPath) {
do {
let pListObject = try PropertyListSerialization.propertyList(from: pListData, options:PropertyListSerialization.ReadOptions(), format:nil)
//Cast pListObject - If expected data type is Dictionary
guard let pListDict = pListObject as? Dictionary<String, AnyObject> else {
return
}
@db42
db42 / navbar.swift
Created October 29, 2017 04:28
Swift 4 recipes: Attributed title for navigation bar
let navLabel = UILabel()
let navTitle = NSMutableAttributedString(string: "PLAIN", attributes:[
NSAttributedStringKey.foregroundColor: UIColor.blue,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17.0, weight: UIFont.Weight.light)])
navTitle.append(NSMutableAttributedString(string: "BOLD", attributes:[
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17.0),
NSAttributedStringKey.foregroundColor: UIColor.blue]))
navLabel.attributedText = navTitle
@db42
db42 / rakefile.rb
Created July 1, 2016 10:41
Create swift extension implementing Argo(Decodable) for JSONModel classes
def parse_properties_txt(fn)
start = false
properties = {}
model = nil
File.read(fn).lines.each do |l|
if l.start_with?("@interface") && l.include?("JSONModel")
model = l.split[1].tr(":","")
start = true