objective-c 如何从 id 获取 BOOL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1230746/
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 22:01:09  来源:igfitidea点击:

How to get BOOL from id

iphoneobjective-ccocoaxcode

提问by Justin Kredible

I'm calling valueForKey on an object. This returns an id which I tried to cast to a BOOL (because I know the value is a BOOL). But XCode gives the following warning:

我在一个对象上调用 valueForKey。这将返回一个我试图转换为 BOOL 的 id(因为我知道该值是一个 BOOL)。但是 XCode 给出了以下警告:

"warning: cast from pointer to integer of different size..."

“警告:从指针转换为不同大小的整数......”

So what I ended up doing was this:

所以我最终做的是这样的:

BOOL value = [[NSNumber numberWithInt:((NSInteger)[managedObject valueForKey:@"fieldName"])] boolValue];

I'm thinking that there must be an easier way. Is there?

我认为必须有一种更简单的方法。在那儿?

回答by Jim Correia

-valueForKey: always returns an object. idis the objective-c type for pointer to any object.

-valueForKey:总是返回一个对象。id是指向任何对象的指针的objective-c 类型。

You can examine the object in the debugger. Some useful commands:

您可以在调试器中检查对象。一些有用的命令:

po value
po [value class]

You'll find that the value is actually an NSNumber, so you can send it the -boolValuemessage to get the BOOL flag you are interested in.

您会发现该值实际上是一个 NSNumber,因此您可以向其发送-boolValue消息以获取您感兴趣的 BOOL 标志。

回答by fulvio

//value = (id)
NSNumber *val = value;
BOOL success = [val boolValue];
if (success) {
 //yay!
}

回答by Chuck

If the value of the key is a BOOL, then the returned object will be an NSNumber. You can just ask it for its boolValue. You can't simply cast from an object (id) to a BOOL or integer or any other non-object type. The KVC machinery autoboxes scalars into the appropriate value type, so you can just ask it for the type you want. You might be interested in the KVC docs— they explain this in more detail.

如果键的值是 BOOL,则返回的对象将是 NSNumber。你可以问问它的boolValue。您不能简单地从对象 ( id) 转换为 BOOL 或整数或任何其他非对象类型。KVC 机器将标量自动装箱为适当的值类型,因此您可以直接询问您想要的类型。您可能对KVC 文档感兴趣- 他们对此进行了更详细的解释。

回答by Ben Leggiero

Easier? Perhaps not. More terse? Not when you asked, but in modern (since at least 2015) Obj-C, yes. If your object is a dictionary with string or number values (NSDictionary<id, StringOrNumber>) of any sort, you can use:

更轻松?也许不是。更简洁?不是在您询问时,而是在现代(至少自 2015 年以来)Obj-C 中,是的。如果您的对象是具有NSDictionary<id, StringOrNumber>任何类型的字符串或数字值 ( ) 的字典,您可以使用:

BOOL value = @(managedObject[@"fieldName"]).boolValue;

Note that, also, if you know the value you get from @"fieldName"is a NSNumber, you can just skip the conversion:

另外请注意,如果您知道从中获得的值@"fieldName"是 a NSNumber,则可以跳过转换:

BOOL value = [managedObject[@"fieldName"] boolValue];


Why?

为什么?

Thanks to some changes in the LLVM compiler:

感谢 LLVM 编译器中的一些更改:

  • NSDictionarys can now be read and written using the familiar dictionary[key]syntax
  • Compatible values can be turned into NSNumbers using the @(value)syntax. more
  • All properties (and methods that take zero arguments and return a value) can be accessed using dot-syntax: object.propertyand object.method.
  • NSDictionary现在可以使用熟悉的dictionary[key]语法读取和写入 s
  • 可以NSNumber使用@(value)语法将兼容值转换为s 。更多的
  • 所有属性(和采用零参数并返回值的方法)都可以使用点语法访问:object.propertyobject.method

There's a lot of nice syntactic sugar, now. I recommend you look into it!

现在有很多不错的语法糖。我建议你看看它!

回答by quemeful

Swift 3

斯威夫特 3

if let boolValue = dict["key"] as? Bool {
    // do something with boolValue
}