NSString concatenate on creation

本小妞迷上赌 提交于 2019-12-24 11:26:22

问题


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:


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




回答2:


I think below lines may help:

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



回答3:


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.




回答4:


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++




回答5:


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



来源:https://stackoverflow.com/questions/21628472/nsstring-concatenate-on-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!