Code Monkey home page Code Monkey logo

sddelegateproxy's Introduction

SDDelegateProxy

When writing code which uses delegates you'll be used to this pattern:

if([delegate respondsToSelector:@selector(someMethodName)]) {
    [delegate someMethodName];
}

In a large project this happens time and time again. SDDelegateProxy is the fix. It proxies the delegate object and passes the messages it can.

To create a delegate proxy:

id<DelegateProtocol> delegateProxy = (id<DelegateProtocol>)[[SDDelegateProxy alloc] initWithDelegate:delegate];

And then to use it:

[delegateProxy someMethodName];

How to use

Simply pull SDDelegateProxy.m and SDDelegateProxy.h into your project and use the two code snippets above. You can message the proxy with any of the selectors defined in the delegate protocol. Attempting to send other selectors will result in a NotImplemented exception. Sending a message to method which is on the protocol, but is not implemented on the delegate object will not result in a error, and will return sensible defaults if appropriate.

Non-void return values

Obviously for void delegate methods, if they're not implemented on the delegate object the proxy can just swallow the invocation. However, this isn't true if you're expecting a return value. The following default return values are used:

Type Default Value
id nil
NSInteger 0
CGFloat 0.0
BOOL NO

Motivation

Used this in several projects to stop having to write the respondsToSelector if statement. Finally decided to open source it as part of Objective-C Hackathon https://objectivechackathon.appspot.com/

sddelegateproxy's People

Contributors

sammyd avatar

Stargazers

Angus H. avatar Jacob Relkin avatar  avatar Kirill Kunst avatar Stanislav Pankevich avatar Stephen Barnes avatar Leonard Pauli avatar

Watchers

 avatar James Cloos avatar 赵瑜瑜 avatar

Forkers

ishaywei

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.