Skip to content

Instantly share code, notes, and snippets.

View sam-w's full-sized avatar

Sam Warner sam-w

View GitHub Profile
@sam-w
sam-w / gist:ceb75a2a5ad078d4607f
Created August 25, 2014 02:57
List available fonts (iOS)
NSLog(@"*** Listing available fonts ***");
for (NSString *familyName in [UIFont familyNames]) {
NSLog(@"Family name: %@", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"\t- Font name: %@", fontName);
}
}
@sam-w
sam-w / gist:c358c90d10b0115bcb3c
Last active August 29, 2015 14:06
Method swizzling via category
SEL originalSelector = @selector(userInterfaceIdiom);
SEL swizzledSelector = @selector(padUserInterfaceIdiom);
Method originalMethod = class_getInstanceMethod([UIDevice class], originalSelector);
Method swizzledMethod = class_getInstanceMethod([UIDevice class], swizzledSelector);
BOOL didAddMethod =
class_addMethod([UIDevice class],
originalSelector,
method_getImplementation(swizzledMethod),
@sam-w
sam-w / gist:a4d930185d73023c84ba
Last active August 29, 2015 14:07
UIApplication (OrientationCorrectedStatusBarFrame)
@implementation UIApplication (OrientationCorrectedStatusBarFrame)
//When device is in landscape orientation, we get CGRect(0, 0, 20, 1024) for statusBarFrame. This corrects the frame based on device orientation.
- (CGRect)orientationCorrectedStatusBarFrame
{
return [self.delegate.window.rootViewController.view convertRect:self.statusBarFrame fromView:self.delegate.window];
}
@end
@sam-w
sam-w / gist:34c721e9888256d50867
Created October 15, 2014 03:13
Copy Autolayout Constraints
+ (void)copyConstraintsFromView:(UIView *)sourceView toView:(UIView *)destinationView
{
NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"firstItem = %@", sourceView];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"secondItem = %@", sourceView];
NSArray *viewConstraints = sourceView.constraints;
for (NSLayoutConstraint *constraint in [viewConstraints filteredArrayUsingPredicate:firstPredicate]) {
NSLayoutConstraint *replacementConstraint = [self duplicateForConstraint:constraint
withFirstItem:destinationView
@sam-w
sam-w / gist:4673e6a67b16769ab05a
Created November 30, 2015 00:02
Reducing over ResultType<Value: CollectionType, Error>
public extension ResultType where Value: CollectionType {
public func reduce<U>(initial: U, @noescape combine: (U, Value.Generator.Element) -> Result<U, Error>) -> Result<U, Error> {
return analysis(
ifSuccess: {
$0.reduce(.Success(initial)) { (acc, item) in
return acc.flatMap {
combine($0, item)
}
}
},
@sam-w
sam-w / ResultExtensions.swift
Created November 30, 2015 00:04
Transforming ResultType<Value: CollectionType, Error> item by item
import Result
public extension ResultType where Value: CollectionType {
/// Returns a new Result by mapping each of `Success`es’ values using `transform`, or re-wrapping `Failure`s’ errors.
public func mapByItem<U>(@noescape transform: Value.Generator.Element -> U) -> Result<[U], Error> {
return flatMapByItem { .Success(transform($0)) }
}
/// Returns the result of applying `transform` to each of `Success`es’ values, or re-wrapping `Failure`’s errors.
public func flatMapByItem<U>(@noescape transform: (Value.Generator.Element) -> Result<U, Error>) -> Result<[U], Error> {
@sam-w
sam-w / gist:3a759c37d824034522ae1a59af6f9374
Created April 30, 2016 06:17
Swift Prime Number Generator
static func getPrimes(to n: Int) -> [Int] {
let xmody = (1...n)
.map { x in (1...n).map { y in x % y } }
let primes = xmody
.map { mods in
mods.enumerate()
.filter { y, mod in mod == 0 }
.map { y, mod in y + 1 } // divisors for x
}
@sam-w
sam-w / BreakTheMemoryDebugger.swift
Created June 14, 2016 01:58
How many objects before Xcode stops detecting retain cycles?
func makeThing(withRefTo inThing: Thing) -> Thing {
let outThing = Thing()
outThing.thing = inThing
return outThing
}
func makeChainOfThings(startingWith inThing: Thing, count: Int) -> Thing {
guard count > 1 else { return inThing }
let outThing = makeThing(withRefTo: inThing)
#delete the existing images
find '/Users/sam.warner/Domain/domain-ios-app/Resources/Images.xcassets/Loading Spinners/Gray' -name "*.png" -type f -delete
for i in `seq 0 9`; do
#edit Contents.json
sed -i '' "s/\"filename\" :.*@2x.*/\"filename\" : \"spinner-loop_0$i.png\",/g" "/Users/sam.warner/Domain/domain-ios-app/Resources/Images.xcassets/Loading Spinners/Gray/loading-spinner-frame0$i.imageset/Contents.json"
#rename the folder
mv "/Users/sam.warner/Domain/domain-ios-app/Resources/Images.xcassets/Loading Spinners/Gray/loading-spinner-frame0$i.imageset" "/Users/sam.warner/Domain/domain-ios-app/Resources/Images.xcassets/Loading Spinners/Gray/gray-spinner-frame0$i.imageset"
#copy the new images
cp ~/spinner-loop/spinner-loop_0$i.png "/Users/sam.warner/Domain/domain-ios-app/Resources/Images.xcassets/Loading Spinners/Gray/gray-spinner-frame0$i.imageset"
@sam-w
sam-w / keys-and-jwts.go
Last active November 21, 2024 13:06
Golang RSA Keys and JWTs
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"time"
"gopkg.in/square/go-jose.v2"