问题
I have a project source code with like 100s of classes that liberally uses hard coded strings throughout the code.
NSArray *sql_stmt = [[NSArray alloc] initWithObjects: DB_QUERY,
[[jsonDictionary objectAtIndex:i] valueForKey:@"RECORD_ID"],
[[jsonDictionary objectAtIndex:i] valueForKey:@"RECORD_LANGUAGE"],
[[jsonDictionary objectAtIndex:i] valueForKey:@"INDEX_PATH"],
[[jsonDictionary objectAtIndex:i] valueForKey:@"KEY"],
[[jsonDictionary objectAtIndex:i] valueForKey:@"VALUE"],nil];
The requirement is to extract all such hardcoded string literals and move it to a class that holds all the constants.
#define RECORD_ID @"RECORD_ID"
#define RECORD_LANGUAGE @"RECORD_LANGUAGE"
etc
It will be too much effort to move all those 100s of strings manually from each class.
Is there a way we could automate this in XCode ? If there is no built in way to do it, I was wondering whether XCode supports writing a macro for this purpose.
Though I can quickly put together a Java or C# based GUI tool where I could paste the source code of one class at a time and get all the strings extracted and replaced by auto-generated name constants, I am hoping to find a solution without having to leave XCode.
来源:https://stackoverflow.com/questions/35188579/moving-objective-c-hard-coded-string-literals-to-constants-automatically