其实问题出在
@property (nonatomic, copy) NSMutableArray* arrayOfObjects;
应该是:
@property (nonatomic, strong) NSMutableArray* arrayOfObjects;
是吗?谢谢你们!
我不完全了解这个错误是如何工作的,但我会尝试了解:)
一个多小时以来,我一直在尝试解决一个问题……恐怕这很简单,但我还不明白。我猜这是内存管理的东西,因为这绝对是我的弱点。
发布一些类似于 “无法识别的选择器发送到实例” 和其他一些但他们没有解决我的问题......
简而言之(粘贴很多原因不知道潜在的错误在哪里):
@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 的数据库:
数据收集.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-03-05 15:42:56.826 XMLTest[11787:207] 初始化前的记录数:0 当前语言:自动;目前objective-c 2013-03-05 15:43:51.446 XMLTest[11787:207]-[__NSArrayI addObject:]:无法识别的选择器发送到实例0x6816110
你看到我做错了吗?我猜这是内存管理的东西