Giter Site home page Giter Site logo

weak's Introduction

Weak

Build Status Platform codecov Swift Xcode SPM MIT

Weak and Unowned as native Swift types. inspire by https://github.com/nvzqz/Weak

The Problem

Swift allows for weak and unowned bindings to objects out-of-the-box, but they can't be used just anywhere.

  1. They can't be used as associated values of enum cases.

  2. They can't be passed as a parameter to a function, method, or initializer without increasing the reference count.

Weak solves these two problems by giving you full control over how you want to pass around weak or unowned references.

Installation

Compatibility

  • Platforms:
    • macOS 10.9+
    • iOS 8.0+
    • watchOS 2.0+
    • tvOS 9.0+
    • Linux
  • Xcode 8.0
  • Swift 3.0

Install Using Swift Package Manager

The Swift Package Manager is a decentralized dependency manager for Swift.

  1. Add the project to your Package.swift.

    import PackageDescription
    
    let package = Package(
        ...
        dependencies: [
          ...
            .Package(url: "https://github.com/ytyubox/Weak.git", from: "1.0.0"),
        ],
        targets: [
        .target(
            name: "...",
            dependencies: ["Weak"])
    )
  2. Import the Weak module.

    import Weak

Usage

Weak References

A Weak<T> instance acts just how weak var foo: T would. When the reference count hits 0, the object property becomes nil.

let weakRef: Weak<SomeClass>

do {
    let instance = SomeClass()
    weakRef = Weak(instance)
    print(weakRef.object == nil)  // false
}

print(weakRef.object == nil)  // true

Unowned References

An Unowned<T> instance should only be used when it will not outlive the life of object. Otherwise, accessing object will cause a crash, just like accessing any unowned reference after the reference count hits 0.

let unownedRef: Unowned<SomeClass>

do {
    let instance = SomeClass()
    unownedRef = Unowned(instance)
    print(unownedRef.object)  // SomeClass(...)
}

print(unownedRef.object)  // CRASHES

License

Weak is released under the MIT License.

weak's People

Contributors

ytyubox avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.