NSString 在创建时连接 [英] NSString concatenate on creation

查看:43
本文介绍了NSString 在创建时连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接 2 个字符串,将结果分配给一个新字符串.

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

通常我会这样做:

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

现在我希望 s 是静态的

Now I wish s to be static

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

但是编译器用Initializer 元素不是编译时..."来踢我

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

有没有办法做到这一点?我用谷歌搜索了一下,没有结果,也没有在 StackOverflow 上找到这个问题的答案.

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.

使用像(在 PHP 中)这样的简短形式怎么样

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

$s = $str1.$str2;

任何帮助将不胜感激.

我想要实现的是拥有这样的配置文件(在 PHP 代码中)

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");

我更喜欢在 1 个文件中包含所有配置字符串,并且我在目标 c 中发现了有用的静态字符串.只想把两个有用的东西放在一起:)

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 :)

如果我用定义来获得它就没有米了,但是 NSString 方式是首选,我使用静态只是因为 const 给我带来了一些我还没有解决的编译问题

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

推荐答案

这样解决:

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

谢谢大家.

现在的问题是我必须接受哪一个作为正确答案?我的意思是,我的就是解决方案,但如果没有你们的帮助,我永远不会到达那里

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

这篇关于NSString 在创建时连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆