Skip to content

Instantly share code, notes, and snippets.

// Scanner+Swift.swift
//
// A set of idiomatic swift extensions to Scanner
//
// Based on https://gist.github.com/natecook1000/59bb0c9117b555f5d40d
// Converted to Swift 3
//
import Foundation
@SanjithKanagavel
SanjithKanagavel / RxSwift-RefLinks.txt
Last active January 11, 2019 17:31
RxSwift Links for reference
@kunikullaya
kunikullaya / Date+Extension.swift
Created January 18, 2017 05:21
Date Extension for Swift
import Foundation
extension Date {
func toString(format: String = "yyyy-MM-dd") -> String {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.dateFormat = format
return formatter.string(from: self)
}
@WERUreo
WERUreo / LatLongFromZip.swift
Created January 22, 2017 22:46
This snippet utilizes Google's Geocoding API to get the latitude, longitude, and place name for a given zip code or location. I used it in a Vapor app, which is where the `drop` is coming from.
enum LocationError: Error, LocalizedError
{
case invalidZipcode
case invalidLocation
case invalidAddress
case invalidAPIKey
var errorDescription: String?
{
switch self
@pkuecuekyan
pkuecuekyan / WKWebViewSizeToFit.swift
Last active June 17, 2024 17:44
Adjust height of WKWebView frame based on scrollHeight of the webView's content
// Since the WKWebView has no sizeToFit() method, increase the frame height of the webView to
// match the height of the content's scrollHeight
//
// The WKWebView's `navigationDelegate` property needs to be set for the delegate method to be called
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
if let height = result as? CGFloat {
@mttcrsp
mttcrsp / UITableView+beginRefreshing.swift
Last active February 21, 2025 11:04
Extension that allows programmatic pull to refresh
import UIKit
public extension UITableView {
public func beginRefreshing() {
// Make sure that a refresh control to be shown was actually set on the view
// controller and the it is not already animating. Otherwise there's nothing
// to refresh.
guard let refreshControl = refreshControl, !refreshControl.isRefreshing else {
return
class NativeViewController: UIViewController {
private var welcomeLabel: UILabel!
private var continueButton: UIButton!
private var iPadExclusiveButton: UIButton!
private var continueButtonFullWidthConstraint: NSLayoutConstraint!
private var continueButtonFixedWidthConstraint: NSLayoutConstraint!
private var iPadExclusiveButtonConstraints: [NSLayoutConstraint]!
class MyViewController: UIViewController {
var constraintContainers: [AdaptiveConstraintContainer] = []
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
constraintContainers.forEach { $0.update(for: traitCollection) }
}
override func loadView() {
@matsuda
matsuda / NotificationExtension.swift
Created March 8, 2017 02:29
Notification extensions in Swift 3
extension Notification {
// MARK: - Identifier
enum Identifier: String {
case DidChangeSession
}
// MARK: - UserInfoKey
enum UserInfoKey: String {
case Session