目标 C 布尔数组 [英] Objective C Boolean Array

查看:49
本文介绍了目标 C 布尔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Objective-c 中使用一组布尔值.我已经完成了大部分设置,但是编译器会在以下语句中发出警告:

I need to utilize an array of booleans in objective-c. I've got it mostly set up, but the compiler throws a warning at the following statement:

[updated_users replaceObjectAtIndex:index withObject:YES];

这是,我敢肯定,因为 YES 根本不是一个对象;这是一个原始的.无论如何,我需要这样做,并且非常感谢有关如何完成它的建议.

This is, I'm sure, because YES is simply not an object; it's a primitive. Regardless, I need to do this, and would greatly appreciate advice on how to accomplish it.

谢谢.

推荐答案

是的,就是这样:NS* 容器只能存储 Objective-C 对象,不能存储原始类型.

Yep, that's exactly what it is: the NS* containers can only store objective-C objects, not primitive types.

您应该能够通过将其包装在 NSNumber 中来完成您想要的:

You should be able to accomplish what you want by wrapping it up in an NSNumber:

[updated_users replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:YES]]

或使用 @(YES)BOOL 包装在 NSNumber

or by using @(YES) which wraps a BOOL in an NSNumber

[updated_users replaceObjectAtIndex:index withObject:@(YES)]]

然后你可以拉出 boolValue:

You can then pull out the boolValue:

BOOL mine = [[updated_users objectAtIndex:index] boolValue];

这篇关于目标 C 布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆