• Stars
    star
    330
  • Rank 127,657 (Top 3 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reflection for enumerations in Objective-C.

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
  • get an enumeration's member for a string (if it exists)

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
AccountType accountType = REFMemberForString(accountType, @"AccountTypeAdmin"); // 1 (first argument is just for an enum type indication)

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);
AccountType accountType = REFMemberForString((AccountType)0, @"AccountTypeAdmin"); // 1 (first argument is just for an enum type indication)

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();
AccountType accountType = REFMemberForStringInAccountType((AccountType)0, @"AccountTypeAdmin");

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), REFMaxIn\(enumName) and REFMemberForStringIn\(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:

More Repositories

1

Optimizing-Swift-Build-Times

Collection of advice on optimizing compile times of Swift projects.
Swift
3,579
star
2

MotionBlur

MotionBlur allows you to add motion blur effect to iOS animations.
Objective-C
1,505
star
3

AHKActionSheet

An alternative to the UIActionSheet inspired by the Spotify app.
Objective-C
1,162
star
4

IBAnalyzer

Find common xib and storyboard-related problems without running your app or writing unit tests.
Swift
955
star
5

SloppySwiper

UINavigationController delegate that allows swipe back gesture to be started from anywhere on the screen (not just from the edge).
Objective-C
806
star
6

DeallocationChecker

Catch leaking view controllers without opening Instruments.
Swift
793
star
7

AHKBendableView

UIView subclass that bends its edges when its position changes.
Swift
592
star
8

EmojiTextView

Tap to swap out words with emojis. Inspired by Messages.app on iOS 10.
Swift
338
star
9

ConfigurableTableViewController

Typed, yet Flexible Table View Controller
Swift
269
star
10

AHKNavigationController

A UINavigationController subclass that re-enables the interactive pop gesture when the navigation bar is hidden or a custom back button is used.
Objective-C
232
star
11

HamburgerButton

Animated hamburger button.
Swift
197
star
12

BouncyView

Action Sheet animation based on Skype's iOS app.
Swift
128
star
13

AHKSlider

A UISlider subclass that improves the precision of selecting values.
Objective-C
59
star
14

SwiftOutTimingFunction

SwiftOutTimingFunction brings Google's new *Swift Out* animation curve to iOS.
Objective-C
44
star
15

AHKBuilder

Initialization for immutable objects based on the builder pattern.
Objective-C
39
star
16

SingleFetchedResultController

Like NSFetchedResultsController but for a single managed object.
Swift
26
star
17

StoryboardDependencyInjectionTemplates

Code generation of initializers for storyboard-based view controllers
HTML
19
star
18

BezierCurveFit

A simple app for fitting Bezier curve based on data points from another curve.
Objective-C
9
star
19

sicp

My solutions to exercises from "Structure And Interpretation Of Computer Programs"
Scheme
5
star
20

cool-compiler-examples

MIPS assembler files generated by my COOL compiler
Assembly
4
star
21

epidemic-simulation

Source code of a research project based on the use of cellular automata.
JavaScript
2
star
22

ContentOffsetJump

Source code of my answer: http://stackoverflow.com/a/19344402/1990236
Objective-C
1
star