Giter Site home page Giter Site logo

multidelegate's Introduction

MultiDelegate

MultiDelegate is a delegate multiplexing class for Objective-C. In other words, it will dispatch delegate methods to multiple objects, instead of being restricted to a single delegate object. You can also use it as a generic method dispatch mechanism. For more information see the blog post.

Build Status

Example

Suppose you have a UITableView and you want to implement the data source using two separate classes: one is the actual data source implementing the tableView:numberOfRowsInSection: method and the other one is the cell factory implementing the tableView:cellForRowAtIndexPath: method to construct the cells.

First create an AIMultiDelegate instance. You need to keep a strong reference to this instance because most objects don't retain their delegates:

_multiDelegate = [[AIMultiDelegate alloc] init];

Then add all the actual delegates to the _multiDelegate object:

[_multiDelegate addDelegate:self];
[_multiDelegate addDelegate:_dataSource];

Finally set the table's data source as the delegate multiplexer:

self.tableView.dataSource = (id)_multiDelegate;

See the example project for the full source.

Remarks

Keep this in mind

  • Every method invocation will be forwarded to each object in the list in the order they were added.
  • If a method returns a value the return value will be from the last object that responded to the method. For example if object A implements method getInt by returning 1, object B implements getInt by returning 2 and object C doesn't implement getInt, calling getInt on an AIMultiDelegate containing A, B and C (in that order) will return 2.
  • AIMultiDelegate doesn't keep strong references to the objects added to it.
  • Some objects only call respondsToSelector: when you first set the delegate to improve performance, so make sure you add all your delegates to the AIMultiDelegate before you set it as the delegate.

Installation

If you are using CocoaPods, add this to your Podfile:

pod 'MultiDelegate'

Otherwise add AIMultiDelegate.h/.m to your project.

multidelegate's People

Contributors

alejandro-isaza avatar iosjerryxiao avatar sgoehler 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

multidelegate's Issues

Stability issue

Maybe it is better to store delegates in NSHashTable (available from iOS 6.0) instead of NSArray.
NSHashTable allows to store zeroing weak references, and therefore crashes may be avoid if some of delegates in _delegates collection was already deallocated.

Using NSPointerArray breaks implementation of indexOfObject: for deallocated objects

After the recent change to an NSPointerArray, I noticed I was getting lots of doesNotRecognizeSelector: crashes. After some investigation, I found out the problem. I often remove my delegate from the multidelegate after the former objects have been deallocated โ€“ however, since we are using self-zeroing pointers, indexOfObject: fails since the pointer is already zeroed out. Thus, I am left with a single delegate (that is nil), which crashes when any message is called upon the multidelegate.

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.