iOS4 - 尝试更改字符串值

发布于 2024-12-28 23:10:29 字数 944 浏览 0 评论 0原文

如果字符串值满足某些条件,我需要更改它,但到目前为止我还没有任何运气。我不确定这是输入字符串还是我做了一些根本错误的事情 - 这是我的代码:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableString *stadium = [NSMutableString stringWithString:[prefs stringForKey:@"stadiumname"]]; 

NSLog(@"stadium: %@", stadium);  


if([prefs stringForKey:@"stadiumname"]==@"Brighton & Hove")
{
    [stadium setString:@"Hove"];
}

if([prefs stringForKey:@"stadiumname"]==@"Monmore Green")
{
    [stadium setString:@"Monmore"];
}

NSLog(@"stadium: %@", stadium);

在两侧的 NSLog 输出中,我仍然得到“Brighton & Hove”或“Monmore Green”,而不仅仅是“正如我所期待的那样,“Hove”或“Monmore”。我尝试更改输入字段以允许可能的编码:

NSMutableString *stadium = [NSMutableString stringWithString:[[prefs stringForKey:@"stadiumname"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

但这没有什么区别。 NSLog 会显示字符串中的任何编码吗?

我只需要能够在短绳从另一侧出来后使用它,但到目前为止还没有运气。

谁能启发我吗?

I need to change a string value if it meets certain criteria, but I'm not having any luck so far. I'm not sure if it's the input string or if I'm doing something fundamentally wrong - here's my code:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableString *stadium = [NSMutableString stringWithString:[prefs stringForKey:@"stadiumname"]]; 

NSLog(@"stadium: %@", stadium);  


if([prefs stringForKey:@"stadiumname"]==@"Brighton & Hove")
{
    [stadium setString:@"Hove"];
}

if([prefs stringForKey:@"stadiumname"]==@"Monmore Green")
{
    [stadium setString:@"Monmore"];
}

NSLog(@"stadium: %@", stadium);

In the NSLog output on both sides I still get "Brighton & Hove" or "Monmore Green", rather than just "Hove" or "Monmore" as I was expecting. I've tried changing the input field to allow for possible encoding by doing:

NSMutableString *stadium = [NSMutableString stringWithString:[[prefs stringForKey:@"stadiumname"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

but that makes no difference. Would NSLog show up any encoding in the string anyway?

I just need to be able to use the shortened string once it comes out the other side, but so far no luck.

Can anyone enlighten me?

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

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

发布评论

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

评论(1

柠檬色的秋千 2025-01-04 23:10:29

这是因为你如何比较字符串,所以不要

if([prefs stringForKey:@"stadiumname"]==@"Brighton & Hove")

使用

if([[prefs stringForKey:@"stadiumname"] isEqualToString:@"Brighton & Hove"])

it's because of how you compare the string, so instead of

if([prefs stringForKey:@"stadiumname"]==@"Brighton & Hove")

use

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