Skip to content

Instantly share code, notes, and snippets.

View smic's full-sized avatar

Stephan Michels smic

View GitHub Profile
//
// SMBond+Position.swift
// SMChemSketchSwiftModel
//
// Created by Stephan Michels on 09.01.17.
// Copyright © 2017 Stephan Michels. All rights reserved.
//
import Foundation
import SMGraphics
@smic
smic / BorderlessWindow.swift
Last active July 7, 2023 20:19
Extension to create borderless windows in SwiftUI
import SwiftUI
extension CGRect {
fileprivate func point(anchor: UnitPoint) -> CGPoint {
var point = self.origin
point.x += self.size.width * anchor.x
#if os(macOS)
point.y += self.size.height * (1 - anchor.y)
#else
point.y += self.size.height * anchor.y
@smic
smic / ClassType.swift
Created August 23, 2018 13:01
25: Cannot invoke initializer for type 'Holder<_>' with an argument list of type '(ObjectWithValue)'
class Holder<T: AnyObject> {
var value: T? = nil
init() {
self.value = nil
}
init(_ value: T) {
self.value = value
}
@smic
smic / BadAccess.swift
Created October 20, 2016 19:34
Mysterious Bad Access
import Cocoa
public func with<T>(_ item: T, update: (inout T) throws -> Void) rethrows -> T {
var this = item; try update(&this); return this
}
let path = with(CGMutablePath()) { // <--- Bad Access
$0.move(to: CGPoint(x: 0, y: 0))
$0.addLine(to: CGPoint(x: 100, y: 100))
}
@smic
smic / OperatorConflict.swift
Created June 17, 2016 14:01
Operator conflict if using the * operator for CGPoints
import Cocoa
extension CGPoint {
public func length() -> CGFloat {
return hypot(self.x, self.y)
}
}
infix operator ⋅ { associativity left precedence 140 }
@smic
smic / CGPath+Additions.swift
Created June 17, 2016 07:56
Swift Compiler Crash
import Cocoa
public extension CGPath {
public convenience init(roundedRect rect: CGRect, corner: CGSize) {
self.init(roundedRect: rect, cornerWidth: corner.width, cornerHeight: corner.height, transform: nil)
}
}
import Cocoa
class Object1: Protocol1, CustomStringConvertible {
var description: String { return "<\(String(self.dynamicType)):\(unsafeAddressOf(self))>" }
}
protocol Protocol1: class {
}
let object1 = Object1()
protocol Copyable {
func copy() -> Self
}
class Class1: Copyable {
var variable1: String
init(variable1: String) {
self.variable1 = variable1
}
@smic
smic / LinkedList.swift
Created May 5, 2016 06:54
Example to iterate over a linked list like the responder chain
import Cocoa
let window = NSWindow(contentRect: NSRect(origin: .zero, size: NSSize(width: 500.0, height: 500.0)), styleMask: 0, backing: .Buffered, defer: false)
var responder = window.firstResponder
var responderIndex = 1
repeat {
print("Responder: \(responder)")
import Cocoa
class TestObject {
init() {
print("Init \(self)")
}
deinit {
print("Deinit \(self)")
}