目标C:什么是创建和使用动态布尔数组的最佳方式? [英] Objective C: What is the best way to create and use a dynamic boolean array?

查看:355
本文介绍了目标C:什么是创建和使用动态布尔数组的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在挣扎与创建,访问和从动态布尔数组的一个多星期,现在更新价值的最佳途径。

I have been struggling with the best way of creating, accessing and updating values from a dynamic boolean array for more than a week now.

@interface myDelegate : NSObject
{
   NSMutableArray *aShowNote;
}
@property (nonatomic, copy) NSMutableArray *aShowNote;

这是我是如何初始化我的数组:

This is how I have initialised my array:

NSMutableArray *aShow = [[NSMutableArray alloc] init]; 
for (i=0; i < c; i++) 
    [aShow addObject:[NSNumber numberWithBool:false]];
self.aShowNote = aShow;

这似乎是工作确定,但我很困惑,为什么每个元素都具有相同的地址初始化。

This seems to work OK but I'm baffled why each element is initialised with the same address.

那么是什么我发现在我的研究,到目前为止是看来你需要的,如果你想改变它的值来代替对象:

But then what I've discovered in my research so far is that is seems that you need to replace the object if you want to change its value:

myDelegate *appDelegate = (myDelegate *)[[UIApplication sharedApplication] delegate];
NSInteger recordIndex = 1;

NSNumber *myBoolNo = [appDelegate.aShowNote objectAtIndex:recordIndex];
BOOL showNote = ![myBoolNo boolValue];

[appDelegate.aShowNote replaceObjectAtIndex:recordIndex withObject:[NSNumber numberWithBool:showNote]];

但这种做法似乎只是过度复杂(它崩溃太)。

but this approach just seems to be over complicated (and it crashes too).

终止应用程序由于未捕获的exception'NSInvalidArgumentException',原因:' - [__ NSArrayI replaceObjectAtIndex:withObject:]:无法识别的选择发送到实例0x5b51d00

任何指针,以改善这个code(当然,使其工作)将非常感激地接受。

Any pointers to improve this code (and of course to make it work) would be very gratefully received.

感谢

Iphaaw

推荐答案

问题是,复制在属性复制分配的对象。而副本创建不可变对象。

the problem is that copy in a property copies the assigned object. And copy creates immutable objects.

更改属性来读取: @属性(非原子,保留)的NSMutableArray * aShowNote;

我认为没有太大改善,从我知道这是去,如果你想要一个NSArray与布尔值的方式。

And I think there is not much to improve, from what I know this is the way to go if you want an NSArray with booleans.

这篇关于目标C:什么是创建和使用动态布尔数组的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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