如何在Objective-C中将NSString解析为BOOL ?

时间:2022-09-07 09:46:18

I am programming in Objective-C for iOS. I would like to parse an object of type NSString into a scalar of type BOOL.

我在用Objective-C为iOS编程。我想把NSString类型的对象解析成BOOL类型的标量。

I have a value, and I know that it will either be @"YES" or @"NO", but that YES (or) NO value is NSString and I just want to change NSString into BOOL.

我有一个值,我知道它可以是@"YES"或@"NO",但是YES(或)没有值是NSString,我只想把NSString改为BOOL。

How can I do that?

我怎么做呢?

Please answer me if you know.

如果你知道,请回答我。

Thanks for reading.

感谢你的阅读。

5 个解决方案

#1


143  

I think it's this:

我认为它是这样的:

BOOL boolValue = [myString boolValue];

#2


22  

You should probably use NSString's -boolValue. To quote the documentation directly:

你应该使用NSString的-boolValue。直接引用文件:

[Returns t]he Boolean value of the receiver’s text. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9—the method ignores any trailing characters. Returns NO if the receiver doesn’t begin with a valid decimal text representation of a number.

返回接收者文本的布尔值。当遇到“Y”、“Y”、“T”、“T”或数字1-9时返回YES,该方法将忽略任何尾随字符。如果接收者不以有效的十进制数字表示开头,则返回NO。

That would seem to match your input cases.

这似乎与你的输入情况相符。

#3


2  

if ([string isEqualToString: @"YES"])
  foo();
else
  bar();

#4


0  

This would probably best be solved with a conditional, for example:

这个问题最好是有条件的解决,例如:

if ([myString isEqualToString:@"YES"])
    myBool = YES;
else if ([myString isEqualToString:@"NO"])
    myBool = NO;

Hope this helped you, good luck with your programming.

希望这对您有所帮助,祝您编程顺利。

#5


0  

This property should also return true if string is 'true' that's why i think extension is needed...

如果字符串是“true”,那么这个属性也应该返回true,这就是为什么我认为需要扩展……

extension NSString{
 var boolValueExtended: Bool {
    get{
        return boolValue ||
            self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).uppercaseString == "TRUE"
    }
}

}

}

#1


143  

I think it's this:

我认为它是这样的:

BOOL boolValue = [myString boolValue];

#2


22  

You should probably use NSString's -boolValue. To quote the documentation directly:

你应该使用NSString的-boolValue。直接引用文件:

[Returns t]he Boolean value of the receiver’s text. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9—the method ignores any trailing characters. Returns NO if the receiver doesn’t begin with a valid decimal text representation of a number.

返回接收者文本的布尔值。当遇到“Y”、“Y”、“T”、“T”或数字1-9时返回YES,该方法将忽略任何尾随字符。如果接收者不以有效的十进制数字表示开头,则返回NO。

That would seem to match your input cases.

这似乎与你的输入情况相符。

#3


2  

if ([string isEqualToString: @"YES"])
  foo();
else
  bar();

#4


0  

This would probably best be solved with a conditional, for example:

这个问题最好是有条件的解决,例如:

if ([myString isEqualToString:@"YES"])
    myBool = YES;
else if ([myString isEqualToString:@"NO"])
    myBool = NO;

Hope this helped you, good luck with your programming.

希望这对您有所帮助,祝您编程顺利。

#5


0  

This property should also return true if string is 'true' that's why i think extension is needed...

如果字符串是“true”,那么这个属性也应该返回true,这就是为什么我认为需要扩展……

extension NSString{
 var boolValueExtended: Bool {
    get{
        return boolValue ||
            self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).uppercaseString == "TRUE"
    }
}

}

}