• Stars
    star
    78
  • Rank 412,246 (Top 9 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 6 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Swift micro-framework to easily deal with weak references to self inside closures

WeakableSelf

platforms pod Carthage compatible

Context

Closures are one of Swift must-have features, and Swift developers are aware of how tricky they can be when they capture the reference of an external object, especially when this object is self.

To deal with this issue, developers are required to write additional code, using constructs such as [weak self] and guard, and the result looks like the following:

service.call(completion: { [weak self] result in
    guard let self = self else { return }
    
    // use weak non-optional `self` to handle `result`
})

Purpose of WeakableSelf

The purpose of this micro-framework is to provide the developer with a helper function weakify that will allow him to declaratively indicate that he wishes to use a weak non-optional reference to self in closure, and not worry about how this reference is provided.

Usage

Using this weakify function, the code above will be transformed into the much more concise:

import WeakableSelf

service.call(completion: weakify { strongSelf, result in    
    // use weak non-optional `strongSelf` to handle `result`
})

weakify works with closures that take up to 7 arguments.

Installation

Requirements

  • Swift 4.2+
  • Xcode 10+

CocoaPods

Add the following to your Podfile:

pod "WeakableSelf"

Carthage

Add the following to your Cartfile:

github "vincent-pradeilles/weakable-self"

More Repositories

1

swift-tips

A collection useful tips for the Swift language
Swift
979
star
2

KeyPathKit

KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.
Swift
420
star
3

locatable

A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern
Swift
117
star
4

KeyPathTesting

A Swift framework that defines a KeyPath-based syntax for writing unit tests
Swift
71
star
5

AutoMocker

AutoMocker is a Swift framework that leverages the type system to let you easily create mocked instances of your data types.
Swift
43
star
6

slides

37
star
7

ios-memes

31
star
8

learning-swiftui-when-you-already-know-uikit

Learning SwiftUI, when you already know UIKit
Swift
19
star
9

live-ios-app-architecture

18
star
10

kotlin-tips

A collection useful tips for the Kotlin language
Kotlin
13
star
11

live-async-await-ios13

Swift
11
star
12

workshop-nsspain-2019

Leveraging KeyPaths to their full potential
Swift
10
star
13

live-async-algorithms

9
star
14

NSAttributedStringBuilder

A micro-framework that exposes a function builder to compose attributed strings
Swift
8
star
15

how-to-master-combine-like-a-pro

Swift
8
star
16

euler-kotlin

Standard mathematical operations implemented through type extensions and infix functions
Kotlin
7
star
17

swift-playgrounds-background-remover-app

This is the repository with the code for this livestream πŸ‘‰ https://www.youtube.com/watch?v=gLn_0nan-pw
Swift
5
star
18

experimenting-with-createml-resources

Swift
3
star
19

live-building-your-first-swiftui-app

Swift
3
star
20

learning-uikit-when-you-already-know-swiftui

Swift
3
star
21

swift-island-2023

Swift
2
star
22

how-to-master-modern-swift-syntaxes-keypath

Swift
2
star
23

parallelize-blocking-api

Swift
2
star
24

live-discovering-swift-macros

Swift
1
star
25

swift-playgrounds

Swift
1
star
26

LocaLint

A command-line tool to lint the usage of iOS localized Strings
Swift
1
star
27

reflexive-factory

Swift
1
star
28

live-discovering-swift-data

Swift
1
star
29

loader-table-view

Swift
1
star
30

how-to-get-started-with-swift-macros

How to get started with Swift Macros
Swift
1
star
31

from-async-rx-api

Swift
1
star
32

vincent-pradeilles.github.io

CSS
1
star