Skip to content

Instantly share code, notes, and snippets.

@kdeda
kdeda / View+NSWindow.swift
Last active March 10, 2024 13:31
Open/Close NSWindow from SwiftUI.View
import SwiftUI
/**
hack to avoid crashes on window close, and remove the window from the
NSApplication stack, ie: avoid leaking window objects
*/
fileprivate final class WindowDelegate: NSObject, NSWindowDelegate {
func windowShouldClose(_ sender: NSWindow) -> Bool {
NSApp.removeWindowsItem(sender)
return true
@gonzalezreal
gonzalezreal / KeyedDecodingContainer+EmptyRepresentable.swift
Last active January 11, 2023 08:25
A technique to avoid having optional array properties in your models when decoding JSON using Swift 4
/// A type that has an "empty" representation.
public protocol EmptyRepresentable {
static func empty() -> Self
}
extension Array: EmptyRepresentable {
public static func empty() -> Array<Element> {
return Array()
}
}
@briankc
briankc / NSScreen+CGDirectDisplayID.swift
Last active August 7, 2023 16:38
Get display ID from NSScreen for use with CGDirectDisplay API
import Cocoa
import CoreGraphics
extension NSScreen {
var displayID: CGDirectDisplayID {
return deviceDescription["NSScreenNumber"] as? CGDirectDisplayID ?? 0
}
}
@khanlou
khanlou / MD5StringTest.swift
Last active April 12, 2019 09:18
MD5 and SHA1 on String in Swift 3
import XCTest
@testable import <#project#>
class StringMD5Test: XCTestCase {
func testMD5() {
let string = "soroush khanlou"
XCTAssertEqual(string.md5, "954d741d14b14002d1ba88f600eee635")
//
// UIScrollView+RACSupport.h
// Taskworld
//
// Created by Kittinun Vantasin on 8/5/15.
// Copyright (c) 2015 Taskworld. All rights reserved.
//
@import UIKit;
@hlung
hlung / iOS - constants declaration.m
Last active November 25, 2015 07:41
iOS - how to create constants in private and public scope
// ----------------------------------
//declaration in PRIVATE scope (for using only in your class)
// ----------------------------------
// declare in .m
static const int kTweetMaxCharacters = 140;
static NSString * const kHelloString = @"HELLO!";
// ----------------------------------
// declaration in PUBLIC scope (for using in other classes too)
// ----------------------------------
@calebd
calebd / ArrayHelpers.swift
Last active November 4, 2022 15:17
Swift Helpers
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
@tomasbasham
tomasbasham / UIImage+Scale.m
Last active February 1, 2024 19:04
Scale a UIImage to any given rect keeping the aspect ratio
@implementation UIImage (scale)
/**
* Scales an image to fit within a bounds with a size governed by
* the passed size. Also keeps the aspect ratio.
*
* Switch MIN to MAX for aspect fill instead of fit.
*
* @param newSize the size of the bounds the image must fit within.
* @return a new scaled image.
@claybridges
claybridges / gist:6029091
Last active October 10, 2023 14:26
UIInterfaceOrientationMask vs. UIInterfaceOrientation. As far as I know, a function like this isn't available in the API. I derived this from the enum def for UIInterfaceOrientationMask.
// UIInterfaceOrientationMask vs. UIInterfaceOrientation
// As far as I know, a function like this isn't available in the API. I derived this from the enum def for
// UIInterfaceOrientationMask.
inline BOOL OrientationMaskSupportsOrientation(UIInterfaceOrientationMask mask, UIInterfaceOrientation orientation) {
return (mask & (1 << orientation)) != 0;
}
@eddiemoya
eddiemoya / git-flip-last.sh
Last active September 6, 2018 08:55
Flip the last two commits in a branch using git-rebase, git-cherry-pick, git-update-ref, git-name-rev, and git-rev-parse. Interesting exercise using quite a bit of plumbing commands.
#!/bin/bash
branch=$(git name-rev --name-only HEAD)
git rebase --onto HEAD~2 HEAD~1 HEAD
git cherry-pick ORIG_HEAD~1
git update-ref refs/heads/$branch $(git rev-parse HEAD)
git checkout --quiet $branch
# Instead of creating an independant bash script with the code above,
# consider simply creating a git alias using the command below.