Moving Objective-C hard-coded string literals to constants automatically

北慕城南 提交于 2019-12-11 12:51:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!