Giter Club home page Giter Club logo

reflectableenum's Introduction

ReflectableEnum

Build Status Carthage compatible

A macro and a set of functions introducing reflection for enumerations in Objective-C.

Features:

  • get a string value for an enumeration's member (which is a common problem)
  • get all values used in an enumeration (also a prevalent issue)
  • get a minimum value in an enumeration
  • get a maximum value in an enumeration

Usage

Once you have ReflectableEnum added to your project, you just need to replace existing enum definitions, like:

typedef NS_ENUM(NSUInteger, AccountType) {
  AccountTypeStandard,
  AccountTypeAdmin
};

with:

REFLECTABLE_ENUM(NSInteger, AccountType,
  AccountTypeStandard,
  AccountTypeAdmin
);

Now you can get a string representing an enumerator and all/minimum/maximum values of an enumeration the enumerator belongs to with:

AccountType accountType = AccountTypeStandard;
NSString *typeString = REFStringForMember(accountType);          // @"AccountTypeStandard"
NSArray *allValues = REFAllValuesForEnumWithMember(accountType); // @[@0, @1]
NSInteger mininimum = REFMinForEnumWithMember(accountType);      // 0
NSInteger maximum = REFMaxForEnumWithMember(accountType);        // 1

In case you pass the enumerator directly to one of these functions, you have to cast it to AccountType, because the compiler doesn't know its type (it's treated as NSInteger in this case):

NSString *typeString = REFStringForMember((AccountType)AccountTypeStandard);
NSArray *allValues = REFAllValuesForEnumWithMember((AccountType)AccountTypeStandard);
NSInteger mininimum = REFMinForEnumWithMember((AccountType)AccountTypeStandard);
NSInteger maximum = REFMaxForEnumWithMember((AccountType)AccountTypeStandard);

The need to cast is a hassle, so ReflectableEnum will create enum-specific functions for you too:

NSString *typeString = REFStringForMemberInAccountType(AccountTypeStandard);
NSArray *allValues = REFAllValuesInAccountType();
NSInteger mininimum = REFMinInAccountType();
NSInteger maximum = REFMaxInAccountType();

As you can see names of these functions depend on the name of the enumeration and follow these patterns: REFStringForMemberIn\(enumName), REFAllValuesIn\(enumName), REFMinIn\(enumName) and REFMaxIn\(enumName)

Drawbacks

  • REFStringForMember and REFStringForMemberIn\(enumName) don't work with enumerations containing duplicated values, e.g. with self-referencing members AccountTypeModerator = AccountTypeAdmin
  • can't be used for enumerations already defined in frameworks and libraries

Requirements

  • iOS 7 and above
  • OS X 10.9 and above

Installation

Install with Carthage:

github "fastred/ReflectableEnum"

or with CocoaPods:

pod "ReflectableEnum"

And then import with: #import <ReflectableEnum/ReflectableEnum.h>

Author

Arkadiusz Holko:

reflectableenum's People

Contributors

fastred avatar narfdotpl avatar

Watchers

 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.