将 NSNumber 转换为 NSString 并不是真正的字符串
我在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几个问题
-stringValue 发送到
NSNumber
为您提供对字符串的引用。格式说明符%d
用于 Cint
类型。在这种情况下会发生的是 myString 将包含[myPowerOnOrNot stringValue]
返回的 NSString 的地址。或者,在 64 位上,它将返回该地址的一半。实际上,您可以直接使用[myPowerOnOrNot stringValue]
并避免相对昂贵的-stringWithFormat:
myString
和@"1"
不一定是同一个对象。您的条件仅检查参考文献是否相同。一般来说,对于 Objective-C,您应该使用-isEqual:
来表示对象相等,但我们知道这些是字符串,您可以使用-isEqualToString:
或者甚至更好,这样做将 NSNumber 转换为 int 的数字比较。
最后,如果
myPowerOnOrNot
不应具有 0 或 1 以外的任何值,请考虑使用一个包罗万象的else
来断言或抛出异常,以防万一myPowerOnOrNot
code> 意外地被 bug 设置错误。A couple of problems
-stringValue sent to an
NSNumber
gives you a reference to a string. The format specifier%d
is for the Cint
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:
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:
Or even better, do a numeric comparison of your NSNumber converted to an int.
Finally if
myPowerOnOrNot
is not supposed to have any value other than 0 or 1, consider having a catchallelse
that asserts or throws an exception just in casemyPowerOnOrNot
accidentally gets set wrong by a bug.“myString”是对字符串的引用,而不是字符串本身的值。
== 运算符会将引用与字符串文字进行比较,因此永远不会返回 true。
而是使用
这会将 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
This will compare the value of myString to "1"
在目标 C 中;您无法使用
==
运算符比较字符串是否相等。您在这里要做的就是:
紧凑、快速、美味。
In Objective C; you can't compare strings for equality using the
==
operator.What you want to do here is as follows:
Compact, fast, delicious.