Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
public struct CollectionViewCellProvider {
@available(iOS 13, *)
public static func compositionalCell() {
// compositional collection view cell
}
public static func collectionCell() -> UICollectionViewCell {
// collection view cell
return UICollectionViewCell()
}
}
class ParentViewController: UIViewController {
override func viewDidLoad() {
presentViewController() // No Xcode complains!
}
@discardableResult
func presentViewController() -> UIViewController {
return UIViewController()
}
}
@dynamicCallable
struct RandomProvider {
func dynamicallyCall(withArguments args: [Int]) -> Double {
let numberOfZeroes = Double(args[0])
let maximum = pow(10, numberOfZeroes)
return Double.random(in: 0...maximum)
}
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Double {
let numberOfZeroes = Double(args.first?.value ?? 0)
let maximum = pow(10, numberOfZeroes)
return Double.random(in: 0...maximum)
}
}
let random = RandomProvider()
print(random(1)) // 8.15946134247
print(random(a:1)) // 5.14551692644
@dynamicMemberLookup
struct User {
subscript(dynamicMember member: String) -> String {
let properties = ["firstname": "Bob", "lastname": "Dylan"]
return properties[member, default: ""]
}
}
let user = User()
print(user.firstname) // Bob
print(user.lastname) // Dylan
@main
struct Repeat: ParsableCommand {
@Argument(help: "The phrase to repeat.")
var phrase: String
mutating func run() throws {
for _ in 1...5 {
print(phrase)
}
}
}
class Service {
var closure: (() -> Void)?
func storageExample(with completion: @escaping (() -> Void)) {
closure = completion // error: assigning non-escaping parameter 'completion' to an @escaping closure
}
func asyncExample(with completion: @escaping (() -> Void)) {
DispatchQueue.global().async { // error: escaping closure captures non-escaping parameter 'completion'
completion()
}
}
func runAsyncTask(completion: @escaping (() -> Void)) {
DispatchQueue.global().async {
completion()
}
}
}
enum UILayoutConstraintAxis: Int {
case UILayoutConstraintAxisHorizontal = 0
case UILayoutConstraintAxisVertical = 1
case UILayoutConstraintAxisZ = 2
}
let axis = UILayoutConstraintAxis.UILayoutConstraintAxisHorizontal
switch axis {
case .UILayoutConstraintAxisHorizontal:
print("")
case .UILayoutConstraintAxisVertical:
print("")
@unknown default:
print("")
}
@propertyWrapper
struct WhitespaceTrimmable {
private(set) var value: String = ""
var wrappedValue: String {
get { value }
set { value = newValue.trimmingCharacters(in: .whitespacesAndNewlines) }
}
init(wrappedValue: String) {
self.wrappedValue = wrappedValue
}
}
struct Service {
@WhitespaceTrimmable(wrappedValue: "Default title") var title: String
func accessTitle() {
print("title: \(_title)") //Default title
}
}
var temp = Service()
print(temp.title)
temp.accessTitle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment