无法识别的选择发送到实例

问题描述:

实际上,问题是出在 @property (nonatomic, copy) NSMutableArray* arrayOfObjects;无法识别的选择发送到实例

应该是: @property (nonatomic, strong) NSMutableArray* arrayOfObjects; 是吗?谢谢,那些家伙!

我完全不明白这个bug是如何工作的,但我会努力去了解:)


我已经尝试,因为一个多小时才能解决一个问题......恐怕是非常简单的东西,但我还不明白。我猜这是与内存管理的东西,因为这是我绝对薄弱的一点呢。

张贴类似 'Unrecognized selector sent to instance' 和其他一些人,但他们没有解决我的问题有点......

简而言之(粘贴很多理由不知道潜在的bug是):

@interface MyCustomObject : NSObject { 
    NSString* name; 
    int birthDate; 
    double heightInMeters; 
} 

@property(strong, nonatomic) NSString * name; 
@property(nonatomic) int birthDay; 
@property(nonatomic) double heightInMeters; 

-(id)initWithDate:(int)birthDate 
     AndName:(NSString *)nameString 
     AndHeight:(double)h; 
@end 

//////////////////////////////////

#import "MyCustomObject.h" 

@implementation MyCustomObject 

@synthesize name; 
@synthesize birthDay; 
@synthesize heightInMeters; 

-(id)initWithDate:(int)bd AndName:(NSString *)nameString AndHeight:(double)h{ 

    if(self = [super init]){ 
     self.birthDay = bd; 
     self.name = nameString; 
     self.heightInMeters = h; 
     return self; 
    } 
    return nil; 
} 
@end 

而且某种数据库为MyCustomObject个S:

DataCollection.h:

@interface DataCollection : NSObject{ 

    NSMutableArray* arrayOfObjects; 

} 
@property (nonatomic, copy) NSMutableArray* arrayOfObjects; 

    -(void)addElement:(id)el; 
@end 

而实现:

#import "DataCollection.h" 

    @implementation DataCollection 
    @synthesize arrayOfObjects; 

    -(id)init{ 
     if(self = [super init]){ 
      self.arrayOfObjects = [[NSMutableArray array] init]; 
      NSLog(@"Number of record before init: %d", [self.arrayOfObjects count]); 
      [self addElement:[[MyCustomObject alloc] initWithDate:1988 AndName:@"Georg" AndHeight:1.88]]; 
      [self addElement:[[MyCustomObject alloc] initWithDate:1951 AndName:@"Sebastian" AndHeight:1.58]]; 
      NSLog(@"Number of record before init: %d", [self.arrayOfObjects count]); 
      return self; 
     } 
     return nil; 
    } 

    -(void)addElement:(id)el{ 
     // UNRECOGNIZED SELECTOR ???? 
[self.arrayOfObjects addObject:el]; 
    } 

    @end 

结果是:

2013年3月5日15:42:56.826的XMLTest [11787 :207] 之前的记录数init:0当前语言:auto;目前Objective-C的2013年3月5日 15:43:51.446的XMLTest [11787:207] - [__ NSArrayI ADDOBJECT:]:无法识别 选择发送到实例0x6816110

你明白我做错了吗?我猜这是与内存管理的东西

+1

你为什么要使用(非原子,副本)解决您的问题,而不是(非原子,强)? – johan 2013-03-05 14:54:12

+0

或至少'(nonatomic,retain)' – 2013-03-05 14:55:03

+0

好问题... – 2013-03-05 14:55:24

如果更改

@property (nonatomic, copy) NSMutableArray* arrayOfObjects; 

@property (nonatomic, strong) NSMutableArray* arrayOfObjects; 

这应该

+0

是的,你是对的......谢谢你! – 2013-03-05 15:05:07

+1

您应该提到'copy'会创建一个不可变的副本(因此OP会看到错误),并且不会根据需要实际更新实例变量。 – trojanfoe 2013-03-05 15:05:25

+1

是的,正如trojanfoe所说,当你的@property指令被设置为复制时,你的getter方法实际上会复制你的arrayOfObjects,但它将是NSArray而不是NSMutableArray。 NSArray没有实现addObject:方法,因此导致无法识别的选择器错误。 – 2013-03-05 16:09:39

我没有真正阅读你的问题 - 它太长,但我想我可能知道你的问题。仅查看帖子末尾的调试器输出,看起来您正尝试将addObject:发送到NSArray对象。如果要将对象添加到阵列并将其删除,则需要使用NSMutableArray

+0

不完全是这样,但感谢您的回答; ) – 2013-03-05 15:02:13

+0

是的,肯定是一个偷偷摸摸的问题。 :)关键是 - 相信调试器的消息,跟随它们。这会导致你“为什么我的可变数组被转换为数组?”这会导致你的答案。 :) – jhabbott 2013-03-05 15:50:52

+0

是的,经过一番研究和学习,我发现你的答案并不那么糟糕:D – 2013-03-05 19:23:43