实际上,问题在于
@property (nonatomic, copy) NSMutableArray* arrayOfObjects;
应该是:
@property (nonatomic, strong) NSMutableArray* arrayOfObjects;
是吗?谢谢,那个家伙!
我并不完全理解这个错误是如何工作的,但我会尝试去了解:)
我试图解决一个问题,因为一个多小时......我担心这是非常简单的事情,但我还不明白。我猜这是内存管理的东西,因为这是我的绝对弱点。
发布有点类似于 'Unrecognized selector sent to instance' 和其他一些但他们没有解决我的问题...
简而言之(粘贴很多原因不知道潜在的错误在哪里):
@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
的某种数据库:
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-03-05 15:42:56.826 XMLTest [11787:207]之前的记录数 init:0当前语言:auto;目前客观-c 2013-03-05 15:43:51.446 XMLTest [11787:207] - [__ NSArrayI addObject:]:无法识别 选择器发送到实例0x6816110
你看到我做错了吗?我猜这是内存管理的东西
答案 0 :(得分:3)
如果你改变了
@property (nonatomic, copy) NSMutableArray* arrayOfObjects;
到
@property (nonatomic, strong) NSMutableArray* arrayOfObjects;
那应该解决你的问题
答案 1 :(得分:0)
我实际上没有读过您的问题 - 这太长了,但我想我可能知道您的问题。仅查看帖子末尾的调试器输出,您似乎正在尝试将addObject:
发送到NSArray
对象。如果要向数组添加和删除对象,则需要使用NSMutableArray
代替。