0

In my app, there is a constant in a common class which defined as

#define IMG_INIT_URL @"http://www.xxxx/index.php"

Now i have a requirement of change it as a variable.But my problem is that i used this constant frequently in my code for example:

NSString *imgUrlString = [NSString stringWithFormat:@"%@%@",IMG_INIT_URL,[[[storeDet objectForKey:@"store_imgurls"] objectAtIndex:0]objectForKey:@"simg_imgurl"]];

When i changed it to a variable i have to retreive it through object or class method that requires a lot of editing in my code. So can i maintain the code as same as now(ie, accessing the variable without using object or class method)?

5 Answers 5

1

You could change the define to

#define IMG_INIT_URL [UIApplication.sharedApplication imgInitURL]

and then add a - (NSString *)imgInitURL method in your UIApplication subclass or a UIApplication category. Or add the method to the application delegate if that is more convenient and modify the define accordingly.

3
  • 2
    NSApplication? In iOS? You probably mean UIApplicationDelegate.
    – maroux
    Commented May 27, 2013 at 6:35
  • thanks for your answer. one more thing, this is a root address. i have some more urls defined as constants. now i defined them as static. can i defined them related with IMG_INIT_URL?
    – manujmv
    Commented May 27, 2013 at 6:53
  • @manujmv: that depends on the context of the static variable declaration. If it is inside a method body the above should work. Otherwise it may be possible that the application object is not yet defined. In that case you could define a class method + (NSString *)imgInitURL on some class, and change the define to [MyClass imgInitURL]. In that case you'd also need to include the MyClass header in your apps precompiled (.pch) header, so that the method definition is seen everywhere.
    – Gerd K
    Commented May 27, 2013 at 15:54
1

Assuming you define a property imgURL in a singleton class named Constants, you can do this:

#define IMG_INIT_URL [Constants sharedInstance].imgURL

Also, check out XCode's refactoring under Edit > Refactor.

1

You can always define it in .pch file which is normally found in Supporting Files folder, so you need use it where you want it. I also personally include some classes which i need to use in different classes. Then, i don't need to include in every class that i use.

0

you can use NSUserDefault as below

[[NSUserDefaults standardUserDefaults] setValue:@"http://www.xxxx/index.php" forKey:@"IMG_INIT_URL"];

and access it using whereevr you want.

 NSString *aReturnValue = [[NSUserDefaults standardUserDefaults] valueForKey:@"IMG_INIT_URL"];
0

You can do it through setting the variable in your AppDelegate Class. Create a variable in AppDelegate.h as given,

@property (strong, nonatomic) NSString *imageInitURL;

And synthesis that in AppDelegate .m file

@synthesize imageInitURL;

Then if you want to set the value initially, u can set that in "application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions" method

[self setImageInitURL:@"http://www.xxxx/index.php"];

Then you can access this from any class through appDelegate shared Instance like given,

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate imageInitURL];

The same way you can set new value to this from any class like given

[appDelegate setImageInitURL:@"your new URL"];

Hope this will help you :)

2
  • thx for your reply. but in ur answer, i have to change the code whereever i used this constant. my question is how to avoid that?
    – manujmv
    Commented May 27, 2013 at 6:57
  • 1
    #define is not constant. Its just replacing the name with what you defined for that. Commented May 27, 2013 at 7:21

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.