This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericCache<H: Hashable, V: AnyObject> { | |
let nsCache = NSCache<AnyObject, V>() | |
subscript(_ h: H) -> V? { | |
get { | |
return nsCache.object(forKey: h as AnyObject) | |
} | |
set { | |
if let v = newValue { | |
nsCache.setObject(v, forKey: h as AnyObject) | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// MARK: - typed predicate types | |
public protocol TypedPredicateProtocol: NSPredicate { associatedtype Root } | |
public final class CompoundPredicate<Root>: NSCompoundPredicate, TypedPredicateProtocol {} | |
public final class ComparisonPredicate<Root>: NSComparisonPredicate, TypedPredicateProtocol {} | |
// MARK: - compound operators |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person { | |
let name: String | |
let age: Int | |
} | |
struct Payment { | |
let currency: String | |
let amount: Double | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person { | |
let name: String | |
let age: Int | |
} | |
let persons = [ | |
Person(name: "Pera", age: 20), | |
Person(name: "Mika", age: 30), | |
Person(name: "Laza", age: 40) | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compiles: | |
let goodKeyPath1: KeyPath<Person, String> = \Person.name | |
let goodKeyPath2: KeyPath<Person, Int> = \Person.age | |
// doesn't compile: | |
let badKeyPath = \Person.password |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator ~: MultiplicationPrecedence | |
@discardableResult | |
func ~<U: AnyObject>(object: U, block: (U) -> Void) -> U { | |
block(object) | |
return object | |
} | |
let titleLabel = UILabel() ~ { | |
$0.font = .systemFont(ofSize: 22) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let titleLabel: UILabel = { | |
let l = UILabel() | |
l.font = .systemFont(ofSize: 22) | |
l.textColor = .red | |
l.text = "WELCOME" | |
return l | |
}() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
import Foundation | |
class TestFulfill: XCTestCase { | |
class FulfillOnRelease { | |
let ex: XCTestExpectation | |
init(_ ex: XCTestExpectation) { | |
self.ex = ex | |
} | |
deinit { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension XCTestExpectation { | |
var fulfiller: DeinitBlock { | |
return DeinitBlock { | |
print("Fulfill \(self.description)") | |
self.fulfill() | |
} | |
} | |
} | |
class FillfilerTests: XCTestCase { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func inPredicateBad<S: Sequence>(_ values: S) -> NSComparisonPredicate { | |
let ex1 = NSExpression(forKeyPath: \S.Element.self) | |
let ex2 = NSExpression(forConstantValue: values) | |
return NSComparisonPredicate(leftExpression: ex1, rightExpression: ex2, modifier: .direct, type: .in) | |
} | |
func inPredicateOk<S: Sequence>(_ values: S) -> NSComparisonPredicate { | |
let ex1 = NSExpression.expressionForEvaluatedObject() | |
let ex2 = NSExpression(forConstantValue: values) | |
return NSComparisonPredicate(leftExpression: ex1, rightExpression: ex2, modifier: .direct, type: .in) |