Objective-C Model class + method = SIGABRT

时间:2011-10-06 00:10:52

标签: objective-c class model sigabrt

我知道这是非常基本的,但我无法弄清楚 我有一个基本的模型类(为简单起见,我只留下一些属性)

@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”。我也试过[标记副本],但我想我错过了我的课程的副本实现,我应该实现副本然后在使用addMarker之前复制标记还是还有其他更好的方法吗?
谢谢。

1 个答案:

答案 0 :(得分:0)

除了上面评论主题中的点,你没有正确初始化你的变量,一个猜测是,在你向我们展示的代码中,你错误地将方法addMarker:拼错为{{1 }}