Skip to content

Instantly share code, notes, and snippets.

@danielgalasko
danielgalasko / RepeatingTimer.swift
Last active Sep 10, 2020
A repeating GCD timer that can run on a background queue
View RepeatingTimer.swift
/// RepeatingTimer mimics the API of DispatchSourceTimer but in a way that prevents
/// crashes that occur from calling resume multiple times on a timer that is
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52
class RepeatingTimer {
let timeInterval: TimeInterval
init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}
@danielgalasko
danielgalasko / VersionNumber.swift
Created May 20, 2016
A clean representation of a semantic version number in swift so you can compare version numbers as if they were numbers
View VersionNumber.swift
/**
Represents a semantic version number in the form X.Y.Z
Its most useful feature is that it implements `Comparable`
making for easy comparison checks.
*/
struct VersionNumber {
let version: String
init(version: String) {
View medium-peekandpoppost-0.swift
extension MyViewController {
func registerForPeekAndPopWithCollectionView(collectionView: UICollectionView) {
guard #available(iOS 9.0, *) else { return }
if traitCollection.forceTouchCapability == .Available {
registerForPreviewingWithDelegate(self, sourceView: collectionView)
}
}
}
View medium-peekandpoppost-2.swift
class TweetViewController: UIViewController {
var initiatingPreviewActionController: UIViewController?
override func previewActionItems() -> [UIPreviewActionItem] {
guard let initiatingPreviewActionController = initiatingPreviewActionController else {
assert(false, "Expected initiatingPreviewActionController to be set")
return []
}
return [UIPreviewAction(title: "Share", style: .Default,
handler: {[unowned self] (_, _) -> Void in
let shareSheet = self.createShareSheet()
View medium-peekandpoppost-1.swift
@available(iOS 9.0, *)
extension TwitterFeedViewController: UIViewControllerPreviewingDelegate {
public func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let indexPath = collectionView.indexPathForItemAtPoint(location),
cell = collectionView.cellForItemAtIndexPath(indexPath),
let tweet = viewModel.tweetAtIndexPath(indexPath) else { return nil }
//now we are ready to create our corresponding controller for the tweet
let tweetViewController = self.navigationCoordinationController.tweetControllerForTweet(tweet)
//I will expand on this in a bit but this lets us present controllers using peek and pop actions
tweetViewController.initiatingPreviewActionController = self
@danielgalasko
danielgalasko / NSObject+Description.m
Last active Aug 30, 2016
Using dictionaryWithValuesForKeys to get a great object description
View NSObject+Description.m
- (NSString *)description {
//We list the properties and their values using the ever convenient `dictionaryWithValuesForKeys`
//which will get the values of the corresponding properties
NSDictionary *debugProperties = [self dictionaryWithValuesForKeys:@[NSStringFromSelector(@selector(<#Insert property name#>))]];
return [NSString stringWithFormat:@"<%@: %p> %@", self.class, self, debugProperties];
}
View MagicalRecordLogging.m
//
// 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>
@danielgalasko
danielgalasko / gist:aaf66d1cc11b8aa51f9a
Created Jun 26, 2015
Settings bundle acknowledgements for cocoapods (insert in podfile)
View gist:aaf66d1cc11b8aa51f9a
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
@danielgalasko
danielgalasko / UITableViewAndUICollectionView+IndexPathForSubview.swift
Last active Jan 15, 2020
Easily retrieve the indexPath of cell's subview in UITableView. Great for button taps and TextFields
View UITableViewAndUICollectionView+IndexPathForSubview.swift
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:445b6cfd90cfa56ce3a9
Created May 8, 2015
Generating IPA from .xcarchive
View gist:445b6cfd90cfa56ce3a9
xcodebuild -exportArchive -archivePath $projectname.xcarchive -exportPath $projectname -exportFormat ipa -exportProvisioningProfile “Provisioning Profile Name”
You can’t perform that action at this time.