Concatenate constants Objective-C ios [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create constant NSString by concatenating strings in Obj-C?
I have two constants that I would like to concatenate:
NSString * const WEBSITE_URL = #"http://192.168.1.15:3000/";
NSString * const API_URL = #"http://192.168.1.15:3000/api/";
Normally in other languages I would concatenate the WEBSITE_URL in API_URL, but you can't concatenate a compile time constant since stringWithFormat or anything like it is a runtime, not compile time method.

you can do this with macro
use:
#define WEBSITE_URL #"http://192.168.1.15:3000/"
#define API_URL WEBSITE_URL #"api/"

You could drop to the preprocessor.
#define WEBSITE_URL_DEF "http://192.168.1.15:3000/"
NSString * const WEBSITE_URL = #WEBSITE_URL_DEF;
NSString * const API_URL = #WEBSITE_URL_DEF "api/";

Related

String substitution with variable constants [duplicate]

This question already has answers here:
Objective C Macro append to string
(3 answers)
Closed 6 years ago.
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?
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];
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:
Create a new .m/.c file with a header in Xcode
In the .m/.c file, declare and initialise your constants
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;
}

How to fix duplicate symbol error in Objective-C? [duplicate]

This question already has answers here:
Duplicate Symbol Error in Objective-C build?
(17 answers)
Closed 7 years ago.
the variable is inside Constants.h
ERROR is:
duplicate symbol _OSVShortTermFuelTrim1 in:
/Users/loximity/Library/Developer/Xcode/DerivedData/AutoCodesApp-hjoxbttreaujifdypikhbngdqihd/Build/Intermediates/AutoCodesApp.build/Debug-iphonesimulator/AutoCodesApp.build/Objects-normal/i386/ViewController.o
/Users/loximity/Library/Developer/Xcode/DerivedData/AutoCodesApp-hjoxbttreaujifdypikhbngdqihd/Build/Intermediates/AutoCodesApp.build/Debug-iphonesimulator/AutoCodesApp.build/Objects-normal/i386/FuelTrimViewController.o
and then I am using the above variables in two places, in view controller:
OSVShortTermFuelTrim1 = [NSString stringWithFormat:#"%# %#", [sensor valueStringForMeasurement1:NO], [sensor imperialUnitString]];
and in FuelTrimViewController"
fuelBank1.text = [NSString stringWithFormat:#"%#",OSVShortTermFuelTrim1];
You have to use extern to the declare the constants in the header file:
extern NSString *const YOUR_CONSTANT;
and then in an implementation file (.m), define the value:
NSString *const YOUR_CONSTANT = #"Hello World";
and, incidentally, your example code is the same, for all intents and purposes, to:
fuelBank1.text = OSVShortTermFuelTrim1;

Difference between NSString *const and const NSString *? [duplicate]

This question already has answers here:
Objective-C const NSString * vs NSString * const
(2 answers)
Closed 8 years ago.
Can someone tell me the difference between the following 2 declarations of NSString?
NSString *const userName = #"ABC";
and
const NSString *userName = #"ABC";
The first is a constant pointer to an NSString object, while the second is a pointer to a constant NSString object.
First - A constant pointer (not modifiable) to an NSString (its value can be modified).
Second - A modifiable pointer to a constant NSString (its value can't be modified).

objective-c constants.h static const

I googled a lot of similar questions, but none of them had a really good answer about what i needed.
Im wondering about the best way to write constants in some .h file.
my constants file is just a clear file with no imports, just clear .h file. I import it in prefix file.
when i use
static NSInteger someInteger = 1;
or
static BOOl someBool = YES;
The compiler compiles okay but gives me a warning that this variable is unused even though im using it multiple times in different classes.
But if i use
static NSString* someString = #"";
there are not any warnings.
also if i use
const NSInteger someInteger = 1;
Compiler compiles okay for a real device, but when running on a simulator it does not compile with an error duplicate symbols for architecture i386
also what is the difference between
const NSString* someString = #"";
const NSInteger someInteger = 1;
and
NSString* const someString = #"";
NSInteger const someInteger = 1;
I ended up using static const NSInteger someInteger =1;, but i wonder if this is a right option.
So really my question is: what words should i use to successfully create a constants.h file?
For all types (both primitive or otherwise) then you need to provide a single implementation of it somewhere:
constants.h:
extern const int someValue;
extern NSString * const someString;
constants.m:
const NSInteger someValue = 1;
NSString * const someString = #"Some string";
You never want to use static variables in header files as you will end up with multiple copies of the "constant" in every implementation file that includes that header (they may not upset the linker, and cause a link error, but they are there).

How can I use a String in more than one file? [duplicate]

This question already has answers here:
Constants in Objective-C
(14 answers)
Closed 8 years ago.
in my view controller.m i have a string like this
NSString *valueToSave = #"someValue";
and would like to safe the text with NSUserDefauls in Appdelegate.m
[[NSUserDefaults standardUserDefaults]
setObject:valueToSave forKey:#"preferenceName"];
how can i use the NSString in the other file?
This doesn't work:
#import "viewcontroller.m";
In a header file have extern NSString *valueToSave;. Then in a (1 and only 1) .m file have NSString *valueToSave = #"someValue";
A second option would be to use a #define. Simply put #define kValueToSave (#"someValue") in a header file and use it where you need it.