Skip to content

Instantly share code, notes, and snippets.

View davidahouse's full-sized avatar

David House davidahouse

View GitHub Profile
@davidahouse
davidahouse / UIHostingController+Extension.swift
Created November 19, 2020 12:35
Capture a SwiftUI view to a UIImage
extension UIHostingController {
func capture() -> UIImage {
let size = sizeThatFits(in: UIScreen.main.bounds.size)
view.bounds.size = size
view.sizeToFit()
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
let snapshotImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
@davidahouse
davidahouse / migrator.rb
Created September 29, 2016 11:49
Run the Swift 3 migrator for a project manually from the command line rather than letting Xcode do it
require 'find'
require 'pp'
swift_file_paths = []
Find.find('./') do |path|
if path =~ /.*\.swift$/ and !path.start_with? "./Carthage"
swift_file_paths << path
cmd = "xcrun swift-update -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -target arm64-apple-ios9 #{path} > convert.swift"
system cmd
@davidahouse
davidahouse / Person.swift
Created July 22, 2015 17:13
Simple NSCoding with Swift example, using enum & rawValue for the key names.
@objc(Person)
final public class Person : NSCoding {
public var firstName:String
public var lastName:String
private enum SerializationKeys : String {
case FirstName
case LastName
}
@davidahouse
davidahouse / gist:17f352dfae023e6f337a
Created June 14, 2015 01:31
Trying out arrays filled with things conforming to a Protocol
//: Playground demonstrating how to deal with an Array containing items that conform
//: to a protocol.
import Cocoa
/*:
Just a basic protocol with a single function that has to be implemented.
*/
protocol Nameable {
@davidahouse
davidahouse / gist:783254e6861f87003548
Created May 17, 2015 20:03
NSPathControlItem weirdness
import Cocoa
import AppKit
let item1 = NSPathControlItem()
item1.title = "This is the title"
let title1 = item1.title
@davidahouse
davidahouse / gist:3269f60fe9a9d17649ce
Last active August 29, 2015 14:02
Playing around with Swift and subscripts
import UIKit
extension Array {
subscript(index:Int,defaultValue def:T) -> T {
if ( (0 <= index) && (index < self.count) ) {
return self[index]
}
else {
return def
@davidahouse
davidahouse / gist:3eb5e9dd8bb08972b53a
Created June 21, 2014 21:55
Proposed tag syntax for DHStyleString
//
// Basic replacement of a variable with no specified style attribute
// #{field}
//
// Replacement with variable and style
// #{field:style}
//
// Push a style on the stack
// #{+style}
//
# This is a sample command for compiling a swift file that requires an external framework.
#
#
#
xcrun swift -i swifthello.swift -framework DHTest2 -F /Users/davidahouse/Projects/swift -sdk $(xcrun --show-sdk-path --sdk macosx)
@davidahouse
davidahouse / gist:5477208
Created April 28, 2013 15:25
Upload an image using Rails & Parse
# Add image field to model
# fields :title, :some_image_field
# Create a form for uploading the image
<%= form_tag({ :action => :upload_image_save}, :multipart => true) do %>
<%= file_field_tag 'image' %>
<div class="actions">
<%= submit_tag %>
</div>
@davidahouse
davidahouse / gist:4565614
Created January 18, 2013 16:04
singleton using dispatch_once
+ (<class> *)shared<whatever> {
static <class> *shared<class> = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared<class> = [[self alloc] init];
});
return shared<class>;
}