objective-c BOOL 到 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/738524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:23:25  来源:igfitidea点击:

BOOL to NSString

objective-cnsstringboolean

提问by Craig

If I have a method that returns a BOOL, how do I cast that to an NSStringso I can print it out in console?

如果我有一个返回 a 的方法,BOOL我如何将它转换为 anNSString以便我可以在控制台中打印出来?

For example, I tried doing this, which isn't working:

例如,我尝试这样做,但不起作用:

NSLog(@"Is Kind of NSString:", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");

But I really want to actually turn the return value into an NSString. I know it's a primitive data type, so I can't call methods on it. Do I have to create a string separately and then use the Bool as a parameter in a method on NSString?

但我真的很想将返回值实际转换为 NSString。我知道它是一种原始数据类型,所以我不能调用它的方法。我是否必须单独创建一个字符串,然后在 NSString 的方法中使用 Bool 作为参数?

回答by Andrew Grant

Use a ternary operator:

使用三元运算符

BOOl isKind= [thing isKindOfClass:[NSString class]];

NSLog(@"Is Kind of NSString: %d", isKind);
NSLog(@"Is Kind of NSString: %@", isKind ? @"YES" : @"NO");

回答by Andrew Grant

You need a formatting specifier in your format string:

您的格式字符串中需要一个格式说明符:

NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");

回答by 5bars

In the background BOOL acts like an int type so you can use %i to test for a BOOL type's value in NSLog:

在后台 BOOL 的行为类似于 int 类型,因此您可以使用 %i 在 NSLog 中测试 BOOL 类型的值:

BOOL a = YES;
BOOL b = NO;
NSLog(@"a is %i and b is %i", a, b);

// Output: a is 1 and b is 0

回答by Patrick Perini

So, I know that this is really old, but I thought I might as well toss my solution into the ring. I do:

所以,我知道这真的很旧,但我想我不妨把我的解决方案扔进戒指。我愿意:

#define NSStringFromBOOL(aBOOL)    ((aBOOL) ? @"YES" : @"NO")
NSLog(@"Is Kind of NSString: %@", NSStringFromBOOL([thing isKindOfClass: [NSString class]]);

I feel that this is more in line with some of Apple's to-string macros (NSStringFromClass, NSStringFromRect, NSStringFromSelector, and so on), and generally pretty simple to use on-the-fly. Just be sure to put that macro somewhere globally accessible, or frequently imported!

我觉得这更符合 Apple 的一些 to-string 宏(NSStringFromClassNSStringFromRectNSStringFromSelector等),并且通常非常易于即时使用。只要确保将该宏放在全局可访问或经常导入的地方!

回答by Hot Licks

You print a BOOL like this:

你像这样打印一个 BOOL:

NSLog(@"The BOOL value is %s", theBoolValue ? "YES" : "NO");

Or, with the new @notation, one could do:

或者,使用新的@符号,可以这样做:

NSLog(@"The BOOL value is %@", @(theBoolValue));

回答by Nuoji

NSLog uses a simple printf-style invocation format its text, and your code example is missing the character sequence needed to embed an object.

NSLog 使用简单的 printf 样式调用格式来格式化其文本,而您的代码示例缺少嵌入对象所需的字符序列。

This should work:

这应该有效:

NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");

回答by HammerSlavik

First of all you should add a formatting specifier %@. It should look like this:

首先,您应该添加一个格式说明符%@。它应该是这样的:

NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");

Also you can extract a conversion from BOOLto NSStringwith extern function as Apple did with NSStringFromCGRect, NSStringFromClassetc.

您也可以像 Apple 那样使用 extern 函数提取 from BOOLto的转换,等等。NSStringNSStringFromCGRectNSStringFromClass

Create utils file or add to existing ones header the following code:

创建 utils 文件或将以下代码添加到现有的文件头:

//NSString+TypeConversion.h

extern NSString *NSStringFromBOOL(BOOL aBool);

And also add the following code into implementation:

并将以下代码添加到实现中:

//NSString+TypeConversion.m

NSString *NSStringFromBOOL(BOOL aBool)
{
    return aBool ? @"YES" : @"NO";
}

So now you can use this function in other places and your code become more clear and reusable:

所以现在你可以在其他地方使用这个函数,你的代码变得更加清晰和可重用:

#import "NSString+TypesConversion.h"

NSLog(@"Is Kind of NSString: %@", NSStringFromBOOL([thing isKindOfClass:[NSString class]]));

回答by user2941395

This is work for me:

这对我来说是工作:

NSLog(@"The BOOL value is %@", theBoolValue ? "YES" : "NO");