1

I'm trying to concatenate 2 strings assigning the result to a new string.

Normally I would do this way:

NSString * s = [NSString stringWithFormat: @"%@%@", str1, str2]; 

Now I wish s to be static

static NSString * s = [NSString stringWithFormat: @"%@%@", str1, str2];

but compiler kick me with "Initializer element is not a compile-time..."

Is there any way to do this? I Googled a bit with no results and also I have not found answers on StackOverflow asking the question.

And what about using a short form like (in PHP)

$s = $str1.$str2;

Any help will be appreciated.

EDIT: What i want to achieve is to have a config file like this (in PHP code)

define ("BASE_URL", "mysite.com/");
define ("SERVICE_URL1", BASE_URL."myservice1.php?param1=value1");
define ("SERVICE_URL2", BASE_URL."myservice2.php?param2=value2");

I prefer to have all configurations strings in 1 file and i found usefull static strings in objective c. Just want to put 2 usefull thing together :)

EDIT2: There's no metter if i obtain this with defines, but the NSString way is preferred and i use static just beacause const make me some compilation problems i haven't solved yet

1
  • 2
    Where is all this code? In a method? Class level static? Commented Feb 7, 2014 at 13:11

5 Answers 5

3

Use this code for creating static s:

static NSString * s = nil;
if (!s)
    s = [NSString stringWithFormat: @"%@%@", str1, str2];

Also for concatenating two string you case use such code: NSString *s = [str1 stringByAppendingString: str2];

UPDATED: You can concat static string by putting them one by one. Example:

#define STR1 @"First part" @" Second part"
#define STR2 @"Third part " STR1
NSLog(@"%@", STR2);

This cole will print Third part First part Second part

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

6 Comments

why the if(!s) is needed? cannot i simply do s = [NSString stringWithFormat: @"%@%@", str1, str2];?
Ok, and what about the short form? is there any literal way? the problem is that i have all the static strings in an .h file and compiler don't allow me to do if inside
@Alex75, are str1 and str2 constants? How do you define them?
@VitalyS. they are static NSString as well. I can also use #define if needed, but i think there is the same problem
Your code is not useful. A message is sent, a result is /computed/ at /runtime/, every time it is executed, but you want to have it static? Probably you mismatched static and immutable. If you want to create a string at compile time, look at my answer.
|
1

I think below lines may help:

NSString *str1 = @"String1";
NSString *str2 = @"String2";
NSString *combinedStr = [str1 stringByAppendingString:str2];

1 Comment

How does this help? They state they want it to be static so this is incorrect where is the static? -1 I will remove if corrected, and I have tested this it doesn't just mean adding static.
1

If you can use a define, it is pretty simple:

#define A @"a"
#define B @"b"
…
static NSString *ab = A B; // or: @"A" @"B"

You can always concatenate string literals with a single space.

But something very important has to happen to use defines. What's wrong with computing it non-static or compute it once?

BTW: You should use dispatch_once() and not if. For the reasons you can search "dispatch_once" on SO.

Comments

0

If you don't mind compiling Objective-C++ code, you could simply change the extension from .m to .mm, by default XCode compiles according to file type, and this is valid in Objective-C++

1 Comment

Really don't know wich are the difference between OBJC and OBJC++ and wich is the advantage to use one instead the other, but for now i'll go with OBJC since this is an almost finished project for my company
0

Solved this way:

#define kBaseURL @"mysite.com/"
static NSString *kServiceUrl1 = kBaseURL @"myservice1.php?param1=value1";
static NSString *kServiceUrl2 = kBaseURL @"myservice2.php?param2=value2";

thanks all.

now the question is wich one i have to accept as right answer? I mean, mine is the solution, but I would never have got there without your help guys

2 Comments

Definetly Vitali's: First he init's a static with an if instead of dispatch_once() and then he copied my answer in an update. This has to be honored. ;-)
i have red the answer linked by him and basically is no wrong, but the if and the dispatch_once solutions is for a method usage and not what i need exactly. My solution don't need a method so i'll accept mine, but put an up sign to yours since them was useful :)

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.