Giter Club home page Giter Club logo

dynamiccategoryproperties's Introduction

Dynamic Accessors for Category Properties

This is the source code to complement the blog post I wrote.

Using the methods defined in this NSObject category, you can simplify implementation of properties in your own categories.

Simply add the NSObject+DZLCategoryProperties to your project and in any category that you have declared properties.

If you have a category interface as in the following example:

@interface Person (XYZAdditions)
@property (strong, nonatomic) NSString *xyz_nickname;
@property (strong, nonatomic) Company *xyz_employer;
@end

The simply declare the properties as dynamic and call from +load one of the methods in this NSObject+DZLCategoryProperties category to implement the getters and setters for your properties at runtime.

@implementation Person (XYZAdditions)
@dynamic nickname;
@dynamic employer;

+ (void)load
{
  // implement for all properties (where methods are not already defined)
  [self DZL_implementDynamicPropertyAccessors];
  
  // or implement only for specific property specified by name
  [self DZL_implementDynamicPropertyAccessorsForPropertyName:@"xyz_nickname"];
  
  // or implement only for specific properties matching regular expression
  [self DZL_implementDynamicPropertyAccessorsForPropertyMatching:@"^xyz_"];
}

@end

Shorthand

Define DZL_CP_SHORTHAND in your project's precompiled header in order to take advantage of the shorthand versions of the method described above, for example:

In MyAwesomeApp-Prefix.pch

#define DZL_CP_SHORTHAND

This allows you to use the following shorthand methods:

  • +[NSObject implementDynamicPropertyAccessors]
  • +[NSObject implementDynamicPropertyAccessorsForPropertyName:]
  • +[NSObject implementDynamicPropertyAccessorsForPropertyMatching:]

The problem that this solves

Usually, you might define a simple object-type property in a category as follows:

@interface UIView (DZLAdditions)
@property (strong, nonatomic) NSString *name;
@end

static const void *nameKey = &nameKey;

@implementation UIView (DZLAdditions)

- (NSString *)name
{
  return objc_getAssociatedObject(self, nameKey);
}

- (void)setName:(NSString *)name
{
  objc_setAssociatedObject(self, nameKey, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

If you add multiple properties, you have to define the getter and setter for each property. And the worst bit is that all these getters and setters will look almost identical.

#Installing

Add the NSObject+DZLCategoryProperties directory to your project, and optionally define DZL_CP_SHORTHAND in your project's precompiled header file.

Finally...

If you like this project, why not follow me on twitter?

dynamiccategoryproperties's People

Contributors

johnmckerrellav avatar samdods avatar

Watchers

 avatar  avatar  avatar

Forkers

andrey-str

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.