Skip to content

Instantly share code, notes, and snippets.

View krzyzanowskim's full-sized avatar

Marcin Krzyzanowski krzyzanowskim

View GitHub Profile
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)
func XCTAssertThrowsError2<T>(_ expression: () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, _ errorHandler: (Error) -> Void = { _ in }) {
do {
_ = try expression()
XCTFail(message())
} catch {
errorHandler(error)
}
}
@krzyzanowskim
krzyzanowskim / ambiguous.swift
Created June 11, 2019 21:58
throwing closure parameter results in different resolution
class A {}
// foo
func foo<T>(_ v: T, handler: () -> Void) -> T {
print("regular")
return v
}
func foo<T>(_ v: T?, handler: () throws -> Void) rethrows -> T? {
@krzyzanowskim
krzyzanowskim / crash.swift
Last active June 10, 2019 10:18
swift-DEVELOPMENT-SNAPSHOT-2019-06-02
// https://bugs.swift.org/browse/SR-10906
import Foundation
protocol ViewDataSource: class {
func foo<T>() -> [T]
}
class View {
weak var delegate: ViewDataSource?