#define value in stringFormat?
Question
I have a define:
hashdefine kPingServerToSeeIfInternetIsOn "http://10.0.0.8"
then in code I with to use it:
NSString *theURL = [NSString stringWithFormat:@"%@", kPingServerToSeeIfInternetIsOn];
I get an exception.
What's the best way to define the const for the application and use it in a NSString
init?
Solution
Create a header file, e.g. MyAppConstants.h
. Add the following:
extern NSString * const kPingServerToSeeIfInternetIsOn;
In the definition, e.g. MyAppConstants.m
, add:
NSString * const kPingServerToSeeIfInternetIsOn = @"http://10.0.0.8";
In your class implementation, add:
#import "MyAppConstants.h"
You can use the constant as you have done already.
OTHER TIPS
You've #defined it as a C string.
If you want it as an Objective-C String, you need
#define kPingServerToSeeIfInternetIsOn @"http://10.0.0.8"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow