NSMutableArray addObject: - [__ NSArrayI addObject:]:发送到实例的无法识别的选择器 [英] NSMutableArray addObject: -[__NSArrayI addObject:]: unrecognized selector sent to instance

查看:323
本文介绍了NSMutableArray addObject: - [__ NSArrayI addObject:]:发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从周日初始化我的NSMutableArray 100种方式,并且NOTHING正在为我工​​作。我尝试将它设置为等于新分配和初始化的NSMutableArray,只是分配,初始化变量本身,我能想到的每个组合,并且总是相同的结果。

I have tried to initialize my NSMutableArray 100 ways from Sunday, and NOTHING is working for me. I tried setting it equal to a newly allocated and initialized NSMutableArray, just allocating, initializing the variable by itself, every combination I could think of and always the same result.

这里是代码:

Object.h

NSMutableArray *array;

@property (copy) NSMutableArray *array;

Object.m

@synthesize array;

if ( self.array ) {
    [self.array addObject:anObject];
}
else {
    self.array = [NSMutableArray arrayWithObjects:anObject, nil];
}

注意:在调试中,anObject在执行时不是nil。 。

NOTE: In debug "anObject" is NOT nil at time of execution...

我已经测试了anObject,它是初始化的工作正常,但是当我尝试addObject:to self.array时,我不断收到以下错误。

I have tested anObject and it isThe initialization works just fine, but I keep getting the error below when I try to addObject: to self.array.


2010-07-10 11:52:55.499 MyApp [4347:1807] - [__ NSArrayI addObject:]:无法识别的选择器发送到实例0x184480

2010-07-10 11:52:55.499 MyApp[4347:1807] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x184480

2010-07-10 11:52:55.508 MyApp [4347:1807] ***因未捕获的异常而终止应用
'NSInvalidArgumentException',原因:' - [ __NSArrayI addObject:]:无法识别的选择器发送到实例0x184480'

2010-07-10 11:52:55.508 MyApp[4347:1807] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x184480'

有没有人知道出了什么问题?

Does anyone have any idea what's going wrong?

推荐答案

我想向Georg Fritzsche致敬。我最终需要使用(复制)而不是(保留),如果没有他的输入,我就不知道该怎么做。

I would like to tip my hat to Georg Fritzsche. I did end up needing to use (copy) instead of (retain), and I would not have known what to do without his input.

//@property (copy) NSMutableArray *array;
@property (nonatomic, copy) NSMutableArray *array; //overridden method is non-atomic as it is coded and should be reflected here.

如果你想在可变对象上使用(复制),你必须覆盖setter方法跟随...

If you wish to use (copy) on a mutable object you must override the "setter" method as follows...

- (void)setArray:(NSArray *)newArray {

    if ( array != newArray ) { 
        [array release];
        array = [newArray mutableCopy];
//      [array retain]; // unnecessary as noted by Georg Fritzsche
    }

    return;
}

注意:您将收到编译器警告:不兼容的Objective-C类型初始化' struct NSArray *',expected'truct NSMutableArray *'我选择将newArray参数声明为(NSArray *),因为您可以灵活地将任何数组传递并正确复制到您的(NSMutableArray *)变量。如果你想将newArray参数声明为(NSMutableArray *),你仍然需要保留mutableCopy方法以获得你想要的结果。

NOTE: You will get a compiler warning: Incompatible Objective-C types initializing 'struct NSArray *', expected 'struct NSMutableArray *' I chose to declare the newArray parameter as an (NSArray *), because you are given the flexibility to have any array passed and correctly copied to your (NSMutableArray *) variable. If you wish to declare the newArray parameter as an (NSMutableArray *) you will still need to leave the mutableCopy method in place to get your desired results.

欢呼来到Georg!
Z @ K!

Cheers to Georg! Z@K!

这篇关于NSMutableArray addObject: - [__ NSArrayI addObject:]:发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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