gpt4 book ai didi

ios - NSString 在创建时连接

转载 作者:行者123 更新时间:2023-12-01 17:54:00 26 4
gpt4 key购买 nike

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

通常我会这样做:

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

现在我希望 s 是静态的
static NSString * s = [NSString stringWithFormat: @"%@%@", str1, str2];

但是编译器用“Initializer element is not a compile-time ...”踢我

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

以及使用像(在 PHP 中)这样的简短形式怎么样?
$s = $str1.$str2;

任何帮助将不胜感激。

编辑:我想要实现的是拥有这样的配置文件(在 PHP 代码中)
define ("BASE_URL", "mysite.com/");
define ("SERVICE_URL1", BASE_URL."myservice1.php?param1=value1");
define ("SERVICE_URL2", BASE_URL."myservice2.php?param2=value2");

我更喜欢将所有配置字符串放在一个文件中,并且我在 objective-c 中发现了有用的静态字符串。只想把两个有用的东西放在一起:)

EDIT2:如果我通过定义获得这个没有办法,但是首选 NSString 方式,我使用静态只是因为 const 让我遇到了一些我还没有解决的编译问题

最佳答案

使用此代码创建静态 s :

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

对于连接两个字符串,您也可以使用这样的代码: NSString *s = [str1 stringByAppendingString: str2];
更新:
您可以通过将它们一一放置来连接静态字符串。
例子:
#define STR1 @"First part" @" Second part"
#define STR2 @"Third part " STR1
NSLog(@"%@", STR2);

这个油菜将打印 Third part First part Second part

关于ios - NSString 在创建时连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21628472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com