Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
@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
}
class Foo {
struct Flags: OptionSet {
let rawValue: Int
static let one = Flags(rawValue: 1 << 0)
static let two = Flags(rawValue: 1 << 1)
static let three = Flags(rawValue: 1 << 2)
}
private(set) var _flags: Flags = [.one, .two]
@propertyWrapper
struct Wrap<T> {
var wrappedValue: T?
}
class Foo {
@Wrap()
var name: String
}
//
// MultilineLabel.swift
//
// Created by Marcin Krzyzanowski on 19/09/2019.
//
import UIKit
public class MultilineLabel: UILabel {
override public init(frame: CGRect) {
@krzyzanowskim
krzyzanowskim / dictionary_key.swift
Last active August 22, 2019 22:25
Use typed keys
// instead doing this
let dict: Dictionary<String, Any> = [:]
dict["name"] = "Marcin"
// do this if it's closed set
enum Keys: String, CaseIterable {
case name, address
}
// or this if it's open set
@krzyzanowskim
krzyzanowskim / macros.c
Created July 18, 2019 17:25
clang predefined macos
$ clang -x c++ -E -dM -o - /dev/null
#define OBJC_NEW_PROPERTIES 1
#define _LP64 1
#define __APPLE_CC__ 6000
#define __APPLE__ 1
#define __ATOMIC_ACQUIRE 2
#define __ATOMIC_ACQ_REL 4
#define __ATOMIC_CONSUME 1
#define __ATOMIC_RELAXED 0
#define __ATOMIC_RELEASE 3
// https://twitter.com/krzyzanowskim/status/1149607371204784129
extension TimeInterval {
static func minutes(_ minutes: Int) -> TimeInterval {
return TimeInterval(seconds(60) * TimeInterval(minutes))
}
static func seconds(_ seconds: Int) -> TimeInterval {
return TimeInterval(seconds)
}
@krzyzanowskim
krzyzanowskim / titleFontDescriptor.swift
Last active October 20, 2020 18:32
Private "NSCTFontUIUsageAttribute" overwrites public "NSFontFamilyAttribute" attribute 🙃😌 if both set.
// https://twitter.com/krzyzanowskim/status/1142800558480351232
// Private "NSCTFontUIUsageAttribute" overwrites public "NSFontFamilyAttribute" attribute 🙃😌 if both set.
extension UIFontDescriptor.AttributeName {
static let fontUIUsageAttribute = UIFontDescriptor.AttributeName.init(rawValue: "NSCTFontUIUsageAttribute")
}
extension UIFontDescriptor {
class func preferredFontDescriptor(withTextStyle style: UIFont.TextStyle, family: String) -> UIFontDescriptor {
let preferredFontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style).withFamily(family)