Giter Site home page Giter Site logo

weakarray's Introduction

Swift WeakArray

Version 0.3
WeakArray offers a collection type that behaves like a Swift Array, but will not retain any of its objects.

Released under the Apache License, version 2.0

How to Install

  1. Add WeakArray as a submodule with git submodule add https://github.com/dmauro/WeakArray (ideally forking and pointing to your fork's url)
  2. Open the WeakArray folder, and drag WeakArray.xcodeproj into the file navigator of your app project. NOTE: The WeakArray project needs to be added somewhere under the target project or you won't be able to add it to your target dependencies.
  3. Ensure that the deployment target of the WeakArray project matches that of the application target.
  4. In your target's "Build Phases" panel, add WeakArray.framework to the "Target Dependencies"
  5. Click on the + button at the top left of the panel and select "New Copy Files Phase". Rename this new phase to "Copy Frameworks", set the "Destination" to "Frameworks", and add WeakArray.framework.

If that's too complicated, you can always just copy the files into your project manually and add them to your target. You'll only need the WeakArray.h and WeakArray.swift files.

Usage

First make sure to import WeakArray:

import WeakArray

When creating a weak array, specify the collection type as you would with an Array:

typealias myType = AnyObject
var a: WeakArray<MyType> = []

You can also use array literals to create a WeakArray, but if you specify any values you'll need to unwrap your optionals with !:

var view: UIView? = UIView()
var a: WeakArray<UIView> = [view!]

Otherwise a WeakArray works much like an Array:

var a: WeakArray<AnyObject> = []

var view: UIView? = UIView()

// Append
a.append(view)

// Replace with index
a[0] = view

// Replace with range
a[0...1] = [view, view]

// Remove Last
a.removeLast()

// Remove at index
var view: UIView? = a.removeAtIndex(0)

// Insert at index
a.insert(view, atIndex: 0)

// isEmpty and count
if (!a.isEmpty) {
	println("Count: \(a.count)")
}

// First and Last
var first: AnyObject? = a.first
var last: AnyObject? = a.last

// Append WeakArray
var b: WeakArray<AnyObject>() = []
a += b

// Or append a standard library Array
var c: [AnyObject] = []
a += c

Most of the methods you'll find in the Slice struct are implemented on WeakArray, so if there's an Array method you need, it is probably implemented. If you find a need for any of the few that are not implemented, let me know.

You can also enumerate over a WeakArray like you would an Array, but keep in mind it will skip over nil values while count will include them, so count may not match the number of enumerations you get.

func testIterationIsNotInterruptedByNilObject() {
    var a = WeakArray<UIView>()
    var obj1: UIView? = UIView()
    var obj2: UIView? = UIView()
    var obj3: UIView? = UIView()
    a.append(obj1)
    a.append(obj2)
    a.append(obj3)
    obj2 = nil
    var i = 0     
    for value in a {
        i++
    }
    XCTAssert(a.count == 3, "")
    XCTAssert(i == 2, "")
}

Version History

v0.3

  • Update for Swift 1.0. Thanks to Mazyod in Pull Request #1.
  • Implemented more Slice methods.
  • Added equality operator overload.

v0.2

  • Update to match beta 5 array changes
  • Use ArrayLiteralConvertible

v0.1

  • Released!

weakarray's People

Contributors

dmauro avatar mazyod avatar

Stargazers

Kyle Howells avatar

Watchers

Sim Saëns 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.