Skip to content

Instantly share code, notes, and snippets.

View danielctull's full-sized avatar

Daniel Tull danielctull

View GitHub Profile
@danielctull
danielctull / condensed-pure.swift
Last active August 29, 2015 14:28
Using Optional.Some() to condense two guard statements into one.
var phone: String? {
guard
let phone = phoneNumberTextField?.text,
let phoneDetector = try? NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue),
let range = pure(NSRange(location: 0, length: phone.startIndex.distanceTo(phone.endIndex))),
let matches = pure(phoneDetector.matchesInString(phone, options: [], range: range))
where matches.count > 0
else {
return nil
public extension NSIndexPath {
// Allows us to split NSIndexPaths
public subscript (subrange: Range<Int>) -> NSIndexPath? {
guard subrange.startIndex >= 0 else {
return nil
}
@danielctull
danielctull / Example.swift
Last active August 29, 2015 14:24
Use generics to get a child view controller of a given type
if let tabViewController = self.contentViewController?.childViewControllerWithType(NSTabViewController) {
// tabViewController is an NSTabViewController, woop!
}
@danielctull
danielctull / gist:3f60aeff0bd3de1bca46
Created June 7, 2015 09:53
Arranging to meet up on Twitter.
a: “We’re in X!”
b: “Where is everyone?”
c: “Hey we’re in Y!”
d: “Hey we’re in Z!! Come join us!”
c: “@a Oh, we tweeted at the same time, want to come to Y or we can head to X?”
a: “@c We’ll head to you guys”
b: “@a A few of us will head to you at X”
import HealthKit
public extension HKWorkout {
public func predicateForSamplesDuringActivePeriods() -> NSPredicate {
let subpredicates = activePeriods.map { start, end -> NSPredicate in
HKQuery.predicateForSamplesWithStartDate(start, endDate: end, options: .StrictStartDate | .StrictEndDate)
cd "$PROJECT_DIR"
infoPlist="${SRCROOT}/${INFOPLIST_FILE}"
infoPath="${infoPlist%.*}"
infoName=$(basename "${infoPath}")
infoHeader="${infoPath}.h"
infoImplementation="${infoPath}.m"
infoHeaderImport=$(basename "${infoHeader}")
rm -f "$infoHeader"
@import Foundation;
typedef void(^DCTAuthPlatformCompletion)(BOOL success);
typedef void(^DCTAuthPlatformURLOpener)(NSURL *URL, DCTAuthPlatformCompletion completion);
typedef void(^DCTAuthPlatformExpirationHandler)();
typedef id(^DCTAuthPlatformBeginBackgroundTaskBlock)(DCTAuthPlatformExpirationHandler expirationHandler);
typedef void(^DCTAuthPlatformEndBackgroundTaskBlock)(id identifier);
@interface DCTAuthPlatform : NSObject
@danielctull
danielctull / gist:88651165fdc9a7130ed9
Last active August 29, 2015 14:11
Apple's example for the rest of us…
if (error) {
// In your app, handle this error really beautifully.
NSLog(@"An error occured in %@: %@", NSStringFromSelector(_cmd), error);
abort();
}
// In your app, handle this error in an awe-inspiring way.
// In your app, masterfully handle this error.
// In your app, handle this error like a pro.
// In your app, handle this error gracefully.
@import UIKit;
@interface ASRKeyboardNotificationValues : NSObject
- (instancetype)initWithNotification:(NSNotification *)notification;
@property (nonatomic, readonly) NSNotification *notification;
@property (nonatomic, readonly) NSTimeInterval animationDuration;
@property (nonatomic, readonly) UIViewAnimationCurve animationCurve;
@property (nonatomic, readonly) UIViewAnimationOptions animationOptions;
CGRect PSPDFRectSafeInset(CGRect rect, CGFloat dx, CGFloat dy) {
CGRect insetRect = CGRectInset(rect, dx, dy);
if (CGRectIsNull(insetRect)) {
insetRect = CGRectMake(CGRectGetMidX(rect), CGRectGetMidY(rect), 0.f, 0.f);
}
return insetRect;
}