NSString stringWithFormat

发布于 2024-08-30 09:53:43 字数 406 浏览 3 评论 0原文

我不知道我在这里缺少什么。我正在尝试使用 [NSString stringWithFormat] 函数连接字符串。这就是我正在做的事情。

NSString *category = [row objectForKey:@"category"];
NSString *logonUser = [row objectForKey:@"username"];
user.text = [NSString stringWithFormat:@"In %@ by %@", category, logonUser];

这里的问题是它总是只打印一个变量。假设类别中有“Sports”并且 logonUser 中有“Leo”,它将打印“In Sports”并跳过其余文本。它应该打印“In Sports by Leo”。

I don't know what I am missing here. I am trying to concatenate strings using [NSString stringWithFormat] function. This is what I am doing.

NSString *category = [row objectForKey:@"category"];
NSString *logonUser = [row objectForKey:@"username"];
user.text = [NSString stringWithFormat:@"In %@ by %@", category, logonUser];

The problem here is that it always print only one variable. Say if there is "Sports" in category and "Leo" in logonUser it will print "In Sports" and skip the remaining text. It should print "In Sports by Leo".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

机场等船 2024-09-06 09:53:43

user 是 UILabel 吗?确保您的文本没有换行或被剪裁。尝试使 UILabel 更大。

Is user a UILabel? Make sure that your text isn't wrapping or being clipped. Try making the UILabel bigger.

山色无中 2024-09-06 09:53:43

您需要尝试:

NSLog(@"In %@ by %@", category, logonUser);

检查问题!让我知道调试器控制台上的结果 XD

You need to try:

NSLog(@"In %@ by %@", category, logonUser);

To check the problem! Let me know the results on debugger console XD

我是男神闪亮亮 2024-09-06 09:53:43

代码看起来是正确的:

您是否有机会在类别变量中得到回车符或额外的空格?如果标签很小,它可能不会显示完整的字符串。尝试交换第三行中的两个变量,看看输出是什么。

我感到困惑的是,输出中甚至缺少“by”。我有一种感觉,类别变量的值掩盖了文本。

The code looks correct:

By any chance are u getting a carriage return or extra white space in the category variable? In case of a small label, it may not display the full string. Try swapping the two variables in the third line and see what the output is.

I am baffled that even the "by" is missing from the output. I have a feeling that the value of the category variable is masking the text.

有木有妳兜一样 2024-09-06 09:53:43

这段代码的第一行有什么意义?看起来和第三行无关?

您是否 100% 确定类别和 logonUser 都已填充在代码中?也许在 user.text = 行之后放置一个 NSLog 语句,并确保它们具有您期望的值,因为您的第三行看起来不错。

编辑

我会尝试更改

user.text = [NSString stringWithFormat:@"In %@ by %@", category, logonUser];

user.text = [NSString stringWithFormat:@"In %@ by %@", @"category", @"logonUser"];

并查看是否输出按登录用户分类。因为它对我来说看起来确实是正确的。

Whats the point for the first line in this code? It seems unrelated to the 3rd line?

Are you 100% sure that both category and logonUser are populated in the code? Perhaps put a NSLog statement right after the user.text = line and make sure they have the values you expect because your 3rd line looks fine.

Edit

I would try changing

user.text = [NSString stringWithFormat:@"In %@ by %@", category, logonUser];

to

user.text = [NSString stringWithFormat:@"In %@ by %@", @"category", @"logonUser"];

and see if that outputs In category by logonUser. Because it sure looks correct to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文