Skip to content

Instantly share code, notes, and snippets.

View SebastianBoldt's full-sized avatar
:octocat:

Sebastian Boldt SebastianBoldt

:octocat:
View GitHub Profile
@SebastianBoldt
SebastianBoldt / Operators.swift
Created October 16, 2016 09:00
Compare Operators added by Swift 3 Converter
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
switch (lhs, rhs) {
case let (l?, r?):
return l < r
case (nil, _?):
return true
default:
return false
}
}
// Won't work
let x: String? = nil
let y: String? = "Hello Optional"
switch (x,y) {
case .some(let a),.some(let b): print("Optional has an value of \(unwrappedX)")
case .none: print("nothing stored inside the enum")
}
let x: String? = "Hello Optional"
switch x {
case .some(let value): print("Optional has an value of \(value)")
case .none: print("nothing stored inside the enum")
}
let x: String? = "Hello Optional"
let y: String? = "Hello Optional"
switch (x,y) {
case let (x?,y?): print("Values: \(x) & \(y)")
default: print("Error")
}
@SebastianBoldt
SebastianBoldt / multicast.swift
Last active June 19, 2018 15:40
Multicast delegate in swift
import Foundation
internal final class MulticastDelegate<T> {
private var delegates = [Weak]()
func add(_ delegate: T) {
if Mirror(reflecting: delegate).subjectType is AnyClass {
let weakValue = Weak(value: delegate as AnyObject)
guard delegates.index(of: weakValue) == nil else {
return
UIModalPresentationStyle iPhone iPad
.fullScreen YES YES
.pageSheet YES NO
.formSheet YES NO
.currentContext YES YES
.custom NO NO
.overFullScreen NO NO
.overCurrentContext NO NO
.blurOverFullScreen  only on tvOS - N/A N/A
.popover YES NO
@SebastianBoldt
SebastianBoldt / CellProvider.swift
Last active March 12, 2020 18:52
CellProvider
let cellProvider: (UICollectionView, IndexPath, Item) -> UICollectionViewCell? = { collectionView, indexPath, item in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = item.color
return cell
}
enum Section {
case section1
case section2
}
struct Item: Hashable {
let name: String
}
class ViewController: UIViewController {
@IBOutlet var collectionview: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
class ViewController: UIViewController {
@IBOutlet var collectionview: UICollectionView!
lazy var dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: self.collectionview,
cellProvider: self.cellProvider)
lazy var cellProvider: (UICollectionView, IndexPath, Item) -> UICollectionViewCell? = { collectionView, indexPath, item in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.backgroundColor = item.color
return cell
}