Skip to content

Instantly share code, notes, and snippets.

@Kr1sso
Created December 9, 2012 11:31
Show Gist options
  • Save Kr1sso/4244369 to your computer and use it in GitHub Desktop.
Save Kr1sso/4244369 to your computer and use it in GitHub Desktop.
Enum debugging automation for Objective C, eg. with a TextExpander snippet
#!/usr/bin/ruby
def NSStringFromEnum(input)
inputArray = input.lines.collect
typeNameLine = inputArray[-1]
typeName = typeNameLine.match(/(\w+);/)[1]
output = "NSString* NSStringFrom#{typeName}(#{typeName} value) {\n switch (value) {\n";
constantLines = inputArray[1..-2]
constantLines.each {|constantLine|
constantName = constantLine.match(/(\w+)/)[1]
output += " case #{constantName}:\n";
output += " return @\"#{constantName}\";\n";
output += " break;\n";
}
output += " default:\n";
output += " return [NSString stringWithFormat:@\"<unknown #{typeName}: %d>\", value];\n";
output + " }\n}"
end
clipboard = %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
print NSStringFromEnum(clipboard)
@Kr1sso
Copy link
Author

Kr1sso commented Dec 9, 2012

As seen on: http://rentzsch.tumblr.com/post/37512716957/enum-nsstring

This makes it super easy to use with TextExpander. Copy paste your enum definition, type your TextExpander command and this will create the enum debugging help in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment