Non-macro, class-based



OCConstants.h

#import

typedef enum kMyEnum
{
kEnum1 = 1,
kEnum = 100,
} myEnumValues;

@interface OCConstants : NSObject {

}

extern int const kExampleConstInt;
extern NSString * const kExampleConstString;

@end


OCConstants.m

#import "OCConstants.h"

@implementation OCConstants

int const kExampleConstInt = 1;
NSString * const kExampleConstString = @"String Value";

@end


To use:

#import "OCConstants.h"

Then, simply call the specific variable you wish to use.

NSString *newString = [NSString stringWithString:kExampleConstString];


Macros



Simply define the constants in a header file, no implementation file is needed here, and then import into other classes as needed. Beyond that, I'm not sure of the benefit to doing this, especially when I see really long macros.

constants.h

#define kLeftMargin 20.0
#define kTopMargin 20.0
#define kRightMargin 20.0
#define kTweenMargin 10.0


Included in the class, visible only to the class



These are placed before the @implementation line in the implementation file.

static NSString * const kCellIdentifier = @"MyIdentifier";
static NSString * const kTitleKey = @"title";
static NSString * const kViewControllerKey = @"viewController";


Convert List of Strings into .h/.m pairs



This is an Applescript, originally intended to take a list of headers from Excel and convert them into needed constants. Any list will do because all of the strings are scrubbed down to the character set {A-Z, a-z, 0-9, _} so that they can be included into Core Data as well. The one caveat is that they strings are not prepared to be Entity Names which require a capital letter for the first character.


property kConstantNames : {"Animals", "Vegetables", "Minerals"}

property kConstantsFileName : "OCConstants_ConstantsGroupTitle"
property kConstantPrepend : "kOC_ConstantsGroupTitle"
property kCommentPlaceholder : "/* constants_group_title */" & return & return

property kValidChars : {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_"}

property pCodeHLines : {}
property pCodeMLines : {}

on run {}

set lastHeader to (count kConstantNames)
repeat with thisHeader from 1 to lastHeader
set theHeader to item thisHeader of kConstantNames

set nameForVariable to SanitizeStringForCode(theHeader) of me
set valueForDeclaration to SanitizeStringForCode(theHeader) of me

set end of pCodeHLines to CreateHLineWithString(nameForVariable) of me
set end of pCodeMLines to CreateMLineWithString(nameForVariable, valueForDeclaration) of me

end repeat

set hLines to JoinList(pCodeHLines, return) of me
set mLines to JoinList(pCodeMLines, return) of me

my WriteHeaderFile(hLines)
my WriteImplementationFile(mLines)

return {hLines, mLines}

end run

(* Since the headers can (and will) be used for Core Data,
we need to clean everything out that isn't a letter,
number, or underscore *)

on SanitizeStringForCode(sourceStr) -- (string) as string

set cleanStr to sourceStr

set lastChr to (count characters in sourceStr)

repeat with thisChr from 1 to lastChr
set theChr to item thisChr of sourceStr
if theChr is not in kValidChars then
set cleanStr to replace_chars(cleanStr, theChr, "_") of me
end if
end repeat

return cleanStr

end SanitizeStringForCode

(*
extern NSString * const kExampleConstString;
NSString * const kExampleConstString = @"String Value";
*)

on CreateHLineWithString(variableName) -- (string) as string
set kCodeHDeclaration to "extern NSString * const "
set kCodeHEndOfLine to ";"
return kCodeHDeclaration & kConstantPrepend & variableName & kCodeHEndOfLine
end CreateHLineWithString

on CreateMLineWithString(variableName, declaredValue) -- (string) as string
set kCodeMDeclaration to "NSString * const "
set kCodeMEqualsString to " = @\""
set kCodeMEndOfLine to "\";"
return kCodeMDeclaration & kConstantPrepend & variableName & kCodeMEqualsString & declaredValue & kCodeMEndOfLine
end CreateMLineWithString

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

on WriteHeaderFile(the_text)
set cocoaImportCall to "#import " & return & return
set this_story to cocoaImportCall & kCommentPlaceholder & the_text & return & return
set this_file to (((path to desktop folder) as text) & kConstantsFileName & ".h")
my write_to_file(this_story, this_file, true)
end WriteHeaderFile

on WriteImplementationFile(the_text)
set headerImportCall to "#import \"" & kConstantsFileName & ".h" & "\"" & return & return
set this_story to headerImportCall & kCommentPlaceholder & the_text & return & return
set this_file to (((path to desktop folder) as text) & kConstantsFileName & ".m")
my write_to_file(this_story, this_file, true)
end WriteImplementationFile

on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file

on JoinList(theList, TheDelimiter)
set AppleScript's text item delimiters to {TheDelimiter}
set TheListAsText to theList as text
set AppleScript's text item delimiters to ""
return TheListAsText
end JoinList