topgun
topgun

Reputation: 2583

Casting UITextField text value to Boolean

I recognize that I have a string expression which I need to cast it to Boolean from UITextField text. Is there any easier way to cast to boolean straight from uitextfield which is only returning Yes or No?

Thanks.

Upvotes: 1

Views: 370

Answers (3)

Paul.s
Paul.s

Reputation: 38728

Check the documentation for NSString

BOOL boolValue = [textField.text boolValue];

Update

It depends what is actually providing the input for the UITextField but I would say if it is user input then @amit's answer is better and you should change your design to use that.

My answer still stands as a good reminder that NSString has nice methods for returning primitive values such as boolValue, intValue, floatValue...etc

Upvotes: 5

Owen Hartnett
Owen Hartnett

Reputation: 5935

BOOL boolValue = [textField.text isEqualToString:@"Yes"] ? YES : NO;

Upvotes: 0

AAV
AAV

Reputation: 3803

Instead of using UITextField, use UISwitch to get a boolean value. According to Apple developer documentation, to get boolean values, you should use UISwitch.

Upvotes: 6

Related Questions