Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
let str = "abc😂"
str.count // 4
str.utf16.count // 5
str.utf8.count // 7
import Foundation
(str as NSString).length // 5
(str as NSString).lengthOfBytes(using: String.Encoding.utf8.rawValue) // 7
@krzyzanowskim
krzyzanowskim / class_Equatable.swift
Created January 14, 2021 19:24
Swift class Equatable need check parent class
class A: Equatable {
let name: String
init(name: String) {
self.name = name
}
static func ==(lhs: A, rhs: A) -> Bool {
return lhs.name == rhs.name
}
import Foundation
class Node: Equatable, CustomStringConvertible {
let value: Int
var children: [Node]
init(value: Int, children: [Node]) {
self.value = value
self.children = children
}
@krzyzanowskim
krzyzanowskim / myview.swift
Last active October 24, 2020 10:16
FB8820682: Since when NSView.setNeedsDisplayInRect does not affects drawRect rectangle
// macOS 11.0 (20A5395g)
// Xcode 12.2 beta 3 (12B5035g)
class MyView: NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
let rect = NSRect(x: 10, y: 10, width: 10, height: 10)
// https://github.com/apple/swift-algorithms
import Algorithms
extension String {
func lineChunks() -> [Self.SubSequence] {
chunked {
!(
($0.isNewline && !$1.isNewline) || ($0.isNewline && $1.isNewline) && !($0 == "\r" && $1 == "\n")
)
}
@krzyzanowskim
krzyzanowskim / .zshrc
Created July 31, 2020 11:37
Close Xcode on git checkout
# .zshrc
# Close Xcode on git checkout
git() {
if [[ $1 == "checkout" ]]; then
osascript -e 'quit app "Xcode"'
command git "$@";
else
command git "$@";
fi;
}
private struct PopUpButton: NSViewRepresentable {
@Binding var selection: String
var content: [String]
func makeNSView(context: Context) -> NSPopUpButton {
let button = NSPopUpButton()
button.imagePosition = .imageLeading
button.usesSingleLineMode = true
button.autoenablesItems = false
@krzyzanowskim
krzyzanowskim / anySatisfy.swift
Last active June 30, 2020 03:09
anySatisfy
public extension Collection {
func anySatisfy(_ predicate: (Self.Element) throws -> Bool) rethrows -> Bool {
try reduce(false) {
try predicate($1) || $0
}
}
}
// Created by Marcin Krzyzanowski
import Foundation
public protocol JSONEncodable: Encodable { }
public extension JSONEncodable {
func toJSON(using encoder: @autoclosure () -> JSONEncoder = JSONEncoder()) throws -> String {
try String(decoding: encoder().encode(self), as: UTF8.self)
}
// Marcin Krzyzanowski
// blog.krzyzanowskim.com
import Foundation
extension URLResourceKey {
static let bookmarkAllPropertiesKey = URLResourceKey("NSURLBookmarkAllPropertiesKey")
static let fileIDKey = URLResourceKey("_NSURLFileIDKey")
static let inode = URLResourceKey.fileIDKey
}