将 NSNumber 转换为 NSString 并不是真正的字符串

发布于 2024-11-08 17:23:10 字数 669 浏览 5 评论 0原文

我在将 NSNumber 值转换为 NSString 时遇到问题,

MyPowerOnOrNot 是一个 NSNumber 女巫,只能返回 1 或 0 并且 myString 是一个 NSString..

myString = [NSString stringWithFormat:@"%d", [myPowerOnOrNot stringValue]];

NSLog(@"%@",myString);
if(myString == @"1") {
    [tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:@"%@",@"ON"]];
}
else if(myString == @"0") {
    [tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:@"%@",@"OFF"]];
}

这有什么问题?

NSLog 在控制台中将 0 或 1 显示为字符串,但我无法在 if 语句中检查它是 1 还是 0?

如果在实际应该跳入语句时没有跳入语句..我真的不明白为什么这不起作用.. 任何帮助都会非常好!

I have got a problem with converting an NSNumber value to an NSString

MyPowerOnOrNot is an NSNumber witch can only return a 1 or 0
and myString is an NSString..

myString = [NSString stringWithFormat:@"%d", [myPowerOnOrNot stringValue]];

NSLog(@"%@",myString);
if(myString == @"1") {
    [tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:@"%@",@"ON"]];
}
else if(myString == @"0") {
    [tablearrayPOWERSTATUS addObject:[NSString stringWithFormat:@"%@",@"OFF"]];
}

What is wrong with this?

The NSLog shows 0 or 1 in the console as a string but I can't check it if it is 1 or 0 in an if statement?

If doesn't jump into the statements when it actually should.. I really don't understand why this doesn't works..
Any help would be very nice!

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

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

发布评论

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

评论(3

奈何桥上唱咆哮 2024-11-15 17:23:10

几个问题

myString = [NSString stringWithFormat:@"%d", [myPowerOnOrNot stringValue]];

-stringValue 发送到 NSNumber 为您提供对字符串的引用。格式说明符 %d 用于 C int 类型。在这种情况下会发生的是 myString 将包含 [myPowerOnOrNot stringValue] 返回的 NSString 的地址。或者,在 64 位上,它将返回该地址的一半。实际上,您可以直接使用 [myPowerOnOrNot stringValue] 并避免相对昂贵的 -stringWithFormat:

if(myString == @"1")

myString@"1" 不一定是同一个对象。您的条件仅检查参考文献是否相同。一般来说,对于 Objective-C,您应该使用 -isEqual: 来表示对象相等,但我们知道这些是字符串,您可以使用 -isEqualToString:

if ([[myPowerOnOrNot stringValue] isEqualToString: @"1"])

或者甚至更好,这样做将 NSNumber 转换为 int 的数字比较。

if ([myPowerOnOrNot intValue] == 1)

最后,如果 myPowerOnOrNot 不应具有 0 或 1 以外的任何值,请考虑使用一个包罗万象的 else 来断言或抛出异常,以防万一 myPowerOnOrNot code> 意外地被 bug 设置错误。

A couple of problems

myString = [NSString stringWithFormat:@"%d", [myPowerOnOrNot stringValue]];

-stringValue sent to an NSNumber gives you a reference to a string. The format specifier %d is for the C int type. What would happen in this case is that myString would contain the address of the NSString returned by [myPowerOnOrNot stringValue]. Or, on 64 bit, it would return half of that address. You could actually use [myPowerOnOrNot stringValue] directly and avoid the relatively expensive -stringWithFormat:

if(myString == @"1")

myString and @"1" are not necessarily the same object. Your condition only checks that the references are identical. In general with Objective-C you should use -isEqual: for equality of objects, but as we know these are strings, you can use -isEqualToString:

if ([[myPowerOnOrNot stringValue] isEqualToString: @"1"])

Or even better, do a numeric comparison of your NSNumber converted to an int.

if ([myPowerOnOrNot intValue] == 1)

Finally if myPowerOnOrNot is not supposed to have any value other than 0 or 1, consider having a catchall else that asserts or throws an exception just in case myPowerOnOrNot accidentally gets set wrong by a bug.

一枫情书 2024-11-15 17:23:10

“myString”是对字符串的引用,而不是字符串本身的值。

== 运算符会将引用与字符串文字进行比较,因此永远不会返回 true。

而是使用

 if( [myString isEqualToString:@"1"] )

这会将 myString 的值与“1”进行比较

"myString " is a reference to a string, not the value of the string itself.

The == operator will compare the reference to your string literal and so never return true.

Instead use

 if( [myString isEqualToString:@"1"] )

This will compare the value of myString to "1"

明媚殇 2024-11-15 17:23:10

在目标 C 中;您无法使用 == 运算符比较字符串是否相等。

您在这里要做的就是:

[tablearrayPOWERSTATUS addObject:([myPowerOnOrNot integerValue]?@"ON":@"OFF"])];

紧凑、快速、美味。

In Objective C; you can't compare strings for equality using the == operator.

What you want to do here is as follows:

[tablearrayPOWERSTATUS addObject:([myPowerOnOrNot integerValue]?@"ON":@"OFF"])];

Compact, fast, delicious.

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