Skip to content

Instantly share code, notes, and snippets.

@danielgalasko
danielgalasko / SwiftSingleton
Last active August 29, 2015 14:09
A swift singleton initialiser that also allows for configuration of the singleton when it is first created.
class var sharedInstance {
struct Static {
static let instance : SingletonObject = SingletonObject()
static var onceToken : dispatch_once_t = 0
}
dispatch_once(&Static.onceToken) {
//perform configuration here
}
return Static.instance
}
@danielgalasko
danielgalasko / CollectionViewFetchedResultsControllerDelegateCompanion.swift
Last active August 29, 2015 14:15
A UICollectionView + NSFetchedResultsControllerDelegate companion designed to assist with managing inserts, updates, moves and deletions into a UICollectionView. Simply instantiate the companion with a fetchedResultsController, supply it with a delegate, and once the delegate calls the `collectionViewFetchedResultsControllerDelegateCompanionDidF…
/// Manages all the updates, inserts and deletes nicely so that you can worry about only the animation
/// @note Doesnt work with section changes as of yet
class CollectionViewFetchedResultsControllerDelegateCompanion: NSObject,NSFetchedResultsControllerDelegate {
var delegate: CollectionViewFetchedResultsControllerDelegateCompanionDelegate
private var changesKeyedByType:[NSFetchedResultsChangeType:[NSIndexPath]] = [:]
init(fetchedResultsController: NSFetchedResultsController,delegate: CollectionViewFetchedResultsControllerDelegateCompanionDelegate) {
self.delegate = delegate
super.init()
@danielgalasko
danielgalasko / UIView+AutoLayout.swift
Last active March 24, 2021 15:47
A UIView extension for common auto layout constraints
extension UIView {
func addConstaintsToPinHorizontalEdgesToSuperView(padding: CGFloat = 0) {
prepareForConstraints()
self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(padding)-[view]-(padding)-|",
options: NSLayoutFormatOptions(0),
metrics: ["padding":padding],
views: ["view":self]))
}
func addConstaintsToPinVerticalEdgesToSuperView(padding: CGFloat = 0) {
@danielgalasko
danielgalasko / UIView+HorizontalSizeClassInfo.swift
Last active August 29, 2015 14:15
Detect if a UIView has a Regular Width (Supports iOS 7). Sometimes its useful to run certain code depending on whether the device has a regular width.
extension UIView {
/// Returns a boolean indicating whether the view's width is currently in regular mode
/// On iOS 7 this will return true when the device is an iPad and false on iPhone
func isHorizontalSizeClassRegularWidth () -> Bool{
if self.respondsToSelector("traitCollection") {
return self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Regular
} else {
return UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
}
}
@danielgalasko
danielgalasko / AppInfo.swift
Last active February 16, 2016 07:48
Retrieve an iOS App version and build number
struct AppInfo {
static func buildNumber() -> String {
return AppInfo.infoDictionary["CFBundleVersion"] as! String
}
static func versionNumber() -> String {
return AppInfo.infoDictionary["CFBundleShortVersionString"]as! String
}
private static var infoDictionary: [NSObject: AnyObject] {
@danielgalasko
danielgalasko / Dispatch_After.swift
Last active June 14, 2017 07:06
dispatch_after in Swift - A top level function for delaying code execution
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
@danielgalasko
danielgalasko / gist:445b6cfd90cfa56ce3a9
Created May 8, 2015 12:54
Generating IPA from .xcarchive
xcodebuild -exportArchive -archivePath $projectname.xcarchive -exportPath $projectname -exportFormat ipa -exportProvisioningProfile “Provisioning Profile Name”
@danielgalasko
danielgalasko / UITableViewAndUICollectionView+IndexPathForSubview.swift
Last active January 15, 2020 13:56
Easily retrieve the indexPath of cell's subview in UITableView. Great for button taps and TextFields
extension UITableView {
/**
Returns an index path identifying the row and section
of the cell containing the provided view
:param: cellSubview A subview of any given UITableViewCell in the table. Typically this is either a `UIButton` or `UITextField`
*/
func indexPathForCellWithSubview(cellSubview: UIView) -> NSIndexPath? {
let cellFrame = convertRect(cellSubview.bounds, fromView: cellSubview)
let cellCenter = CGPoint(x: CGRectGetMidX(cellFrame), y: CGRectGetMidY(cellFrame))
@danielgalasko
danielgalasko / gist:aaf66d1cc11b8aa51f9a
Created June 26, 2015 07:23
Settings bundle acknowledgements for cocoapods (insert in podfile)
post_install do |installer|
require 'fileutils'
FileUtils.cp_r('Pods/Target Support Files/Pods/Pods-Acknowledgements.plist', 'Resources/Other-Sources/Settings.bundle/Acknowledgements.plist', :remove_destination => true)
end
//
// MagicalRecordLogging.h
// MagicalRecord
//
// Created by Saul Mora on 10/4/13.
// Copyright (c) 2013 Magical Panda Software LLC. All rights reserved.
//
#import <MagicalRecord/MagicalRecord+Options.h>