Giter Site home page Giter Site logo

swift-tips's People

Contributors

vincent-pradeilles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-tips's Issues

Suggestions

#45 Getting rid of overabundant [weak self] and guard

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)
        }
    }
}

#07 Implementing the builder pattern with keypaths

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.

Configuration closures keypath

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.