vincent-pradeilles / swift-tips Goto Github PK
View Code? Open in Web Editor NEWA collection useful tips for the Swift language
License: MIT License
A collection useful tips for the Swift language
License: MIT License
Just a little bit another way with the same example
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
postfix operator ~
public postfix func ~ <T>(value: T) -> T.Type {
return T.self
}
extension NSObjectProtocol {
func weak<T>(_ closure: @escaping (Self) -> (T) -> Void) -> (T) -> Void {
return { [weak self] in
guard let self = self else { return }
return closure(self)($0)
}
}
}
class Producer: NSObject {
deinit {
print("deinit Producer")
}
private var handler: (Int) -> Void = { _ in }
func register(handler: @escaping (Int) -> Void) {
self.handler = handler
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: { self.handler(42) })
}
}
class Consumer: NSObject {
deinit {
print("deinit Consumer")
}
let producer = Producer()
func consume() {
producer.register(handler: weak(self~ .handle))
}
private func handle(_ result: Int) {
print("๐ \(result)")
}
}
var consumer: Consumer? = Consumer()
consumer?.consume()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: { consumer = nil })
// This code prints:
// ๐ 42
// deinit Consumer
// deinit Producer
However sometimes you need function to return value and weak will not work. You might want to use unowned:
extension NSObjectProtocol {
func unowned<T, U>(_ closure: @escaping (Self) -> (T) -> U) -> (T) -> U {
return { [ unowned self] in
return closure(self)($0)
}
}
}
Not using keyPath allows to call functions:
import UIKit
extension NSObjectProtocol {
@discardableResult
func apply(_ closure: (Self) -> () ) -> Self {
{ closure(self) }()
return self
}
}
let view = UIView()
let label = UILabel().apply {
$0.textColor = .red
$0.text = "Foo \n Foo"
$0.textAlignment = .right
$0.layer.cornerRadius = 5
$0.sizeToFit()
}
view.addSubview(label)
It is safe because it nonescaping closure.
Hello Vincent, this repo is so useful if you are interested about keypaths.
I have been digging into the argument for the past weeks, but i still cant master the topic as i would like to.
Supposing you have a function to customize an object by using a closure:
func configure<T: UIView>(block: (T)->())-> T{
let obj = T()
block(obj)
return obj
}
using it, will result in something like:
let label: UILabel = configure {
$0.text = "Some"
$0.backgroundColor = .white
}
Although useful, i was wondering if keypaths could help making the syntax more concise.
Would be possible to achieve something like this?
let label: UILabel = configure {
\.text = "Some"
\.backgroundColor = .white
}
#07 Implementing the builder pattern with keypaths
This section is absolutely interesting, and is very close to my concept of clean and concise syntax.
From your tutorial: https://github.com/vincent-pradeilles/swift-tips#generating-all-cases-for-an-enum
Since XCode 10, You can use CaseIterable
following to this tutorial instead of create a function by yourself: https://developer.apple.com/documentation/swift/caseiterable
Thanks for these good suggestions, many of them are really useful!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.