nsstringgen / README.rst


nsstringfromenumgen generates Objective-C functions for converting enums to NSStrings. It uses libclang for parsing.


Assuming you're on a Mac, you need Xcode and the command line tools installed. On other platforms you probably need libclang somewhere where Python finds it.

This software includes a copy of the libclang Python bindings because they aren't installed by Xcode. They require LLVM 3.1 which is included at least in Xcode 4.5.2.


To install, use

$ sudo python ./ install


Call nsstringfromenumgen with one or more Objective-C source files. It parses them and outputs NSStringFromEnum functions for each enumeration found. It tries to be smart with typedefs and enum names and if it finds both names, it produces two functions. It also outputs an extern declaration for each function:

$ nsstringfromenumgen Foo.h
extern NSString* NSStringFromAnEnum(AnEnum v);
NSString* NSStringFromAnEnum(AnEnum v) {
  switch (v) {
    case AnEnumFirstValue: return @"AnEnumFirstValue";
    case AnEnumSecondValue: return @"AnEnumSecondValue";

If your enums have values defined, they will be included too:

extern NSString* NSStringFromValuesEnum(ValuesEnum v);
NSString* NSStringFromValuesEnum(ValuesEnum v) {
  switch (v) {
    case ValuesEnumFirstValue: return [NSString stringWithFormat:@"ValuesEnumFirstValue (%d)", 1];
    case ValuesEnumSecondValue: return [NSString stringWithFormat:@"ValuesEnumSecondValue (%d)", 2];

If your code relies on a prefix header, you need to include it on the command line with the --include flag. You can specify more than one of them if necessary.

You can also instruct nsstringfromenumgen to treat your enums as options for a bitmask by including the --mask flag:

$ nsstringfromenumgen Foo.h --mask
extern NSString* NSStringFromMaskEnum(enum MaskEnum v);
NSString* NSStringFromMaskEnum(enum MaskEnum v) {
  NSMutableArray *bits = [NSMutableArray array];
  if (v & MaskEnumFirstValue) [bits addObject:@"MaskEnumFirstValue"];
  if (v & MaskEnumSecondValue) [bits addObject:@"MaskEnumSecondValue"];
  return [bits componentsJoinedByString:@" | "];


Because libclang is a real C parser, your file needs to make sense. If libclang produces errors, nsstringfromenumgen gives up because the syntax tree tends to be unpredictable in the presence of errors.