Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS convert bool value to string

Tags:

ios

I have a bool value associated with a call I want to pass out of my iOS program as a string. I have tried the following:

  NSString *connectedString = [self.selectedBeacon.isConnected stringValue];

But I'm not getting anything out.

Can someone please correct me?

like image 424
N0xus Avatar asked Oct 25 '25 14:10

N0xus


2 Answers

If isConnected is a BOOL type it shouldn't even compile. If it's NSNumber you should get "1" or "0".

Do you want "YES" to "NO" string? Solution:

A) If a BOOL type:

NSString *connectedString = self.selectedBeacon.isConnected ? @"YES" : @"NO";

B) If NSNumber add a category method to this class like:

- (NSString *)boolValueString {
    // if this contains BOOL value
    return [self boolValue] ? @"YES" : @"NO";
}
like image 66
Maciej Oczko Avatar answered Oct 27 '25 05:10

Maciej Oczko


Converting Bool To String Swift 3 / Swift 4

let boolValue = true
print (String(boolValue))

Log

true
like image 42
Md. Ibrahim Hassan Avatar answered Oct 27 '25 06:10

Md. Ibrahim Hassan