View Lock.swift
struct Lock { | |
private var unfairLock = os_unfair_lock_s() | |
mutating func lock(_ work: () -> Void) { | |
self.lock() | |
work() | |
self.unlock() | |
} | |
mutating func trylock(_ work: () -> Void) -> Bool { |
View AssociatedProperty.swift
import ObjectiveC | |
extension objc_AssociationPolicy { | |
public static let retain = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN | |
public static let retainNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC | |
public static let copy = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY | |
public static let copyNonatomic = objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC | |
public static let assign = objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN | |
} |
View WKUIDelegate.m
#pragma mark - WKUIDelegate | |
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler | |
{ | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil | |
message:message | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { | |
completionHandler(); |
View AllowDecodeFailure.swift
import Foundation | |
/// Allows a property to fail decoding, defaulting to nil instead of surfacing the error. | |
@propertyWrapper | |
struct AllowDecodeFailure<Wrapped>: Codable where Wrapped: Codable { | |
var wrappedValue: Wrapped? | |
init() { | |
self.wrappedValue = nil | |
} |
View IfElseTernary.swift
func _if<T>(_ test: Bool, action: () -> T) -> T? { | |
if test { | |
return action() | |
} else { | |
return nil | |
} | |
} | |
extension Optional { | |
func _else(action: () -> Wrapped) -> Wrapped { |
View Collection+Average.swift
extension Collection where Element: FloatingPoint { | |
func average() -> Element { | |
return self.reduce(Element.zero, +) / Element(self.count) | |
} | |
} | |
extension Collection where Element: BinaryInteger { | |
func average() -> Double { | |
return Double(self.reduce(0, +)) / Double(self.count) | |
} |
View Example.swift
import PlaygroundSupport | |
let limiter = RateLimiter(timeframe: 5) | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
for i in 0..<100 { | |
limiter.perform { | |
print("perform", i) | |
} |
View ParentUpdate.swift
struct Parent: View { | |
@State var count: Int = 0 | |
var body: some View { | |
return VStack { | |
ImageLoadingView(url: URL(string: "https://github.com/recurser/exif-orientation-examples/blob/9c4ccfaea6bfd434ac1c4bb0750ac6fc5848a5f4/Landscape_8.jpg?raw=true")!) | |
Text("counter: \(count)") | |
} | |
.onAppear { | |
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { (timer) in | |
self.count += 1 |
View ReferenceCache.swift
/// The result of a binary search. | |
struct BinarySearchResult<Index> { | |
/// The index for the searched key. | |
/// | |
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true. | |
var index: Index | |
/// Whether the key was found in the collection. | |
/// | |
/// When performing a binary search, even if the value is not found in the collection, an index is returned which can be used to insert a new item at the correct location. |
View BinarySearch.swift
// adapted from https://github.com/raywenderlich/swift-algorithm-club/tree/master/Binary%20Search | |
/// The result of a binary search. | |
struct BinarySearchResult<Index> { | |
/// The index for the searched key. | |
/// | |
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true. | |
var index: Index | |
/// Whether the key was found in the collection. |