-1

This question is related to Swift and Objective-C.

I want to create variables from Constant Strings. So, in future, when I change name of a variable though out app, I just need to change it at one place, it must be changed, wherever it is used.

Example:

I have user_id in 14 files, if I want to change user_id into userID I have to change in all 14 files, but I want to change at once place only.

0

1 Answer 1

1

One way to do this would be to use the Xcode build process and add a script (language can be of your choice, but the default is a BASH script)

  1. Create string constant text file where you define all your variables you want to change in some format that expresses the change you want to make, for example:

    "variable_one_name" = "new_variable_one_name"

Depending on how 'smart' you wanted your script to be you could also list all your variables and include some way of indicating when a variable is not to be replaced.

"variable_one_name" = "new_variable_one_name"
"variable_two_name" = "DO_NOT_CHANGE"
  1. Run a pre build script on you project that reads in the string constant text file and then iterates through your source files and executes the desired replacement. Be careful to limit the directories you search to you OWN source files!

  2. build project...

This would allow you to manage your constants from one place. However it clearly is only going to help you after you have created a project and written some code :)

BASH string replacement

Adding a run script to the Xcode build process

Sign up to request clarification or add additional context in comments.

10 Comments

Nice answer but not what the OP seems to be asking. Personally I generally use TextWrangler which can perform one change across all files. I have also written programs to handle all files in a directory to replace non-ASCII characters and CR -> LF.
@zaph, I can't do this in iOS, while in PHP, it can be done as define("Test", "testabc"); ${Test} = "test value"; echo ${Test}; exit; So, what do you say. any idea?
@fragilecat , I can also do, it using Find and Replace, but what If I use Strings which are coming from server to make variables?
Different question, this is why it is important to provide information on what you are trying to accomplish.
Then you would have to adjust your workflow to accommodate this change. I cannot tell you how since I do not know your current workflow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.