我知道这是非常基本的,但我无法弄清楚。
我有一个基本的模型类(为简单起见,我只留下一些属性)
@interface Marker : NSObject {
NSInteger book_id;
NSString *detail;
NSNumber *lat;
}
@property (nonatomic, assign) NSInteger book_id;
@property (nonatomic, retain) NSNumber *lat;
@property (nonatomic, retain) NSString *detail;
@end
@synthesize book_id, detail, lat;
- (id)init
{
self = [super init];
if (self) {
detail = [NSString alloc];
lat = [NSNumber alloc];
}
return self;
}
- (void) dealloc {
[detail release];
[lat release];
[super dealloc];
}
@end
我有一个简单方法的单例,我想将这个类的实例推送到那个单例数组中,我有这样的
#import "Marker.h"
@interface MarkersSingleton : NSObject {
NSMutableArray *markers;
}
+ (MarkersSingleton *)getInstance;
- (void)addMarker: (Marker *) marker;
@property (nonatomic, retain) NSMutableArray *markers;
@end
#import "MarkersSingleton.h"
@implementation MarkersSingleton
@synthesize markers;
- (id) init{
self = [super init];
if(self){
markers = [[NSMutableArray alloc] init];
}
return self;
}
+ (MarkersSingleton *) getInstance {
static dispatch_once_t pred;
static MarkersSingleton *inst;
dispatch_once(&pred, ^{
inst = [[MarkersSingleton alloc] init];
});
return inst;
}
- (void) addMaker: (Marker *) marker {
[markers insertObject:marker atIndex:[markers count]];
}
- (void) dealloc {
[markers release];
[super dealloc];
}
@end
现在当我尝试
Marker *marker = [[Marker alloc] init];
...一些套装...
[[MarkersSingleton getInstance] addMarker:marker];
我得到“程序接收信号:SIGABRT”。我也尝试过 [marker copy] 但我想我的课程缺少一个复制实现,我应该在使用 addMarker 之前实现复制然后复制标记还是有其他更好的方法?
谢谢。