Skip to content

Instantly share code, notes, and snippets.

Ross Tulloch rosstulloch

Block or report user

Report or block rosstulloch

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:9aae80d96568be8d5996790ac0214810
protocol HasInit {
init()
func sayHi()
}
extension HasInit {
func sayHi() { print("Hi from an instance of \(type(of: self))") }
}
@rosstulloch
rosstulloch / NSClassFromSwift.swift
Created Oct 25, 2017
To instantiate a Swift class dynamically using NSClassFromSwift. This works for ObjC as well as pure Swift classes.
View NSClassFromSwift.swift
class YourClass {
required init() {
}
func saySomething() {
print("Hi!")
}
}
if let classType = NSClassFromString("APPNAMEORMODULE.YourClass") as? YourClass.Type {
@rosstulloch
rosstulloch / Type Casts.swift
Last active Nov 1, 2017
Type Casts in Swift
View Type Casts.swift
func typecast<T,U>(_ a:T) -> U {
var _a = a
return withUnsafePointer(to: &_a) { (ptr:UnsafePointer<T>) -> U in
return UnsafeRawPointer(ptr).load(as: U.self)
}
}
func typecast_inplace<T,U>(_ a:inout T) -> U {
return withUnsafePointer(to: &a) { (ptr:UnsafePointer<T>) -> U in
return UnsafeRawPointer(ptr).load(as: U.self)
@rosstulloch
rosstulloch / File Reference URL in Swift.swift
Created Nov 2, 2017
File Reference URL in Swift.swift
View File Reference URL in Swift.swift
if let refURL = (a as NSURL).perform(#selector(NSURL.fileReferenceURL))?.takeUnretainedValue() as? NSURL {
print(refURL)
}
You can’t perform that action at this time.