iOS4 - 尝试更改字符串值
如果字符串值满足某些条件,我需要更改它,但到目前为止我还没有任何运气。我不确定这是输入字符串还是我做了一些根本错误的事情 - 这是我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为你如何比较字符串,所以不要
使用
it's because of how you compare the string, so instead of
use