0

In my project, I define my urls like such:

#define TERMSURL       @"http://127.0.0.1:8000/terms/"
#define PRIVACYURL     @"http://127.0.0.1:8000/privacy/"
...

Since the root url (http://127.0.0.1:8000/) is always the same, is there a way to set it as a constant, and then use string substitution for the remaining pieces?

For example, in the other files, I could do something like this:

NSString *devBaseUrl = @"http://127.0.0.1:8000/";
NSString *url1 = [NSString stringWithFormat:@"%@terms/", devBaseUrl];

Is there a way to do that for my current approach?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jape
  • 2,861
  • 2
  • 26
  • 58

2 Answers2

0

shared.h

#define TERMSURL       @"http://127.0.0.1:8000/terms/"
#define PRIVACYURL     @"http://127.0.0.1:8000/privacy/"
#define URL_BASE       @"http://127.0.0.1:8000/"

yourClass.m

NSString * stringUrlBase = URL_BASE;
NSString *url1 = [NSString stringWithFormat:@"%@terms/", stringUrlBase];
oremag14jf
  • 326
  • 3
  • 9
0

Sure, you can do that. I have however seen both a #define and an NSString const * const being used before. Defines are easier, and you're probably not going to save that much memory by having constants instead of individual immutable instances of NSString all over the place.

Some advice is to think about how you export the NSString constants. You'll probably want EXTERN_PRIVATE instead of EXTERN, but my sample code will allow all clients of your header to read the string constants you've declared therein.

What you can do:

  1. Create a new .m/.c file with a header in Xcode
  2. In the .m/.c file, declare and initialise your constants
  3. Export the constant as necessary so other compilation units can access it

constants.h

#ifndef constants_h
#define constants_h

// Export the symbol to clients of the static object (library)
#define EXTERN extern __attribute__((visibility("default")))
// Export the symbol, but make it available only within the static object
#define EXTERN_PRIVATE extern __attribute__((visibility("hidden")))
// Make the class symbol available to clients
#define EXTERN_CLASS __attribute__((visibility("default")))
// Hide the class symbol from clients
#define EXTERN_CLASS_PRIVATE __attribute__((visibility("hidden")))
#define INLINE static inline

#import <Foundation/Foundation.h>

EXTERN NSString const * _Nonnull const devBaseUrl;

#endif /* constants_h */

constants.m

#include "constants.h"

NSString const * _Nonnull const devBaseUrl = @"http://127.0.0.1:8000/";

main.m

#import <Foundation/Foundation.h>
#import "constants.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Constant value: %@", devBaseUrl);
        // Prints: Constant value: http://127.0.0.1:8000/
    }
    return 0;
}
Community
  • 1
  • 1
Sean
  • 5,233
  • 5
  • 22
  • 26
  • Always remember to "constantise" variables that don't get mutated, and always set nullability of pointers. You'll thank yourself if you need Swift operability down the road, and you'll also benefit from additional code- correctness features from the Static Analyzer. – Sean Sep 22 '16 at 15:54