Skip to content

Instantly share code, notes, and snippets.

View drumnkyle's full-sized avatar

Kyle Sherman drumnkyle

View GitHub Profile
@drumnkyle
drumnkyle / safeMutation.swift
Created November 30, 2016 04:16
Makes it so you can throw an error and revert any mutation changes in a struct
protocol SafeMutation {
mutating func safeMutate(_ work: () throws -> ()) rethrows
}
extension SafeMutation {
mutating func safeMutate(_ work: () throws -> ()) rethrows {
let old = self
do {
try work()
} catch {
@drumnkyle
drumnkyle / readableXIB-bookmarklet.js
Last active November 15, 2016 02:18
A helper that will take a XIB as a string and make it more human readable
var outletRegex = /<outlet property=\"(.*)\" destination=\"(.*)\" id=\".*\"\/>/g;
var xcodeIDRegex = /id=\"(.*)\".*userLabel=\"(.*)\"/g;
var customClass = /id=\"(.*)\".*customClass=\"(.*)\"/g;
var allDiffs = [].slice.call(document.getElementsByClassName("sidebyside"));
for (i = 0; i < allDiffs.length; i++) {
if (allDiffs[i].getElementsByClassName("filename-row")[0].textContent.includes(".xib")) {
var xibString = allDiffs[i].innerText;
var matches, output = [];
while (matches = outletRegex.exec(xibString)) {
@drumnkyle
drumnkyle / CropImageToCircle.swift
Last active November 29, 2023 10:42
Code to mask a square image to a circle
// UIImage+Utils.swift
// This is just one way to do this.
public func croppedToCircle() -> UIImage {
// Begin a new image that will be the new image with the rounded corners
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let circleWidth = size.width
let radius = circleWidth / 2
@drumnkyle
drumnkyle / swift2_error_handling_1.swift
Created June 15, 2015 05:42
Swift 2 Error Handling with asynchronous closures
// Error Handling //
enum Either<T> {
case Success(T)
case Failure(ErrorType)
func getValue() throws -> T {
switch self {
case .Success(let value):
@drumnkyle
drumnkyle / single_line_closure.swift
Last active August 29, 2015 14:20
Single Line Closure Problem in Swift
dispatch_async(dispatch_get_main_queue()) {
self.object?.doSomething()
return // This is needed for it to compile
}
@drumnkyle
drumnkyle / swift_objc_blocks_blog.swift
Last active August 29, 2015 14:20
Swift/ObjC Blocks
// Objective-C
BOOL (^completion) (NSError *, NSDictionary *) = ^BOOL(NSError *error, NSDictionary *data) {
// code here
return YES;
}
// Swift
let completion: (NSError, [String : AnyObject]) -> Bool = { error, data in
// code here
return true
@drumnkyle
drumnkyle / swift_anyobject_dict.swift
Last active August 29, 2015 14:08
Swift AnyObject Dictionary Problem
var nsDict: [NSObject : AnyObject] = ["test" : 4]
if let value = nsDict["test"] as? Int {
println("It is an Int")
}
// ERROR: 'Int' is not a subtype of '(NSObject, AnyObject)'
if let value = nsDict["test"].1 as? Int {
println("It is an Int")
}
@drumnkyle
drumnkyle / swift_nsmanagedobject_extension.swift
Last active August 29, 2015 14:03
Trying to rewrite a category on NSManagedObject in Swift
// Objective-C
@implementation NSManagedObject (Helpers)
+ (instancetype)createEntity
{
id newObject = [NSEntityDescription
insertNewObjectForEntityForName:[[self class] description]
inManagedObjectContext:[[SACoreDataStack defaultStack]
managedObjectContext]];
return newObject;
}
@drumnkyle
drumnkyle / SwiftGuidedTourEnum.swift
Last active August 29, 2015 14:02
Problem with Swift GuidedTour enum with protocol challenge
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
class SimpleClass: ExampleProtocol {
var simpleDescription: String = "A very simple class."
var anotherProperty: Int = 69105
func adjust() {
simpleDescription += " Now 100% adjusted."
// The difference between declaring a method in a class, a function
// outside of a class, and an init method.
func foo(#a: String, #b:String) -> String {
return a + " " + b
}
foo(a:"Hello", b:"dude")
func foo1(a: String, b: String) -> String {