Skip to content

Instantly share code, notes, and snippets.

@nicholascross
Last active April 2, 2024 13:42
Show Gist options
  • Select an option

  • Save nicholascross/8285cef20b1f5f171557478647cc0cdd to your computer and use it in GitHub Desktop.

Select an option

Save nicholascross/8285cef20b1f5f171557478647cc0cdd to your computer and use it in GitHub Desktop.

Revisions

  1. nicholascross renamed this gist Feb 9, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. nicholascross created this gist Feb 9, 2019.
    3 changes: 3 additions & 0 deletions Example.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    class Example {

    }
    7 changes: 7 additions & 0 deletions WeakBox.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    struct WeakBox<ItemType: AnyObject> {
    weak var item: ItemType?

    init(item: ItemType?) {
    self.item = item
    }
    }
    24 changes: 24 additions & 0 deletions WeakBoxExampleTests.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    //
    // WeakBox.swift
    // Injectable
    //
    // Created by Nicholas Cross on 9/2/19.
    // Copyright © 2019 Nicholas Cross. All rights reserved.
    //

    import XCTest

    class WeakBoxExampleTests: XCTestCase {

    func testWeakDictionary() {
    var weakDictionary: [String: WeakBox<Example>] = [:]

    var example: Example? = Example()
    weakDictionary["example"] = WeakBox(item:example)

    XCTAssertNotNil(weakDictionary["example"]?.item)
    example = nil
    XCTAssertNil(weakDictionary["example"]?.item)
    }

    }