get"无法识别的选择器发送到班级"使用单身人士

时间:2012-10-03 23:09:10

标签: ios

我是ios的新手,我尝试编写一个简单的单例对象来在控制器之间共享数据。这是我的代码:

#import <Foundation/Foundation.h>
#import "SynthesizeSingleton.h";

@interface BSStore : NSObject

+(BSStore *)sharedStore;

@property (nonatomic,strong) NSArray *sharedNotebooks;

@end



#import "BSStore.h"

@implementation BSStore

SYNTHESIZE_SINGLETON_FOR_CLASS(BSStore)

@synthesize sharedNotebooks;

@end

//在AppDelegate中写对象

[BSStore sharedStore].sharedNotebooks = notebooks;

//在ViewController中读取对象

  Notebook *notebook = [[BSStore sharedStore].sharedNotebooks objectAtIndex:indexPath.row];

我得到了:

2012-10-04 02:01:29.053 BarneyShop[1827:f803] +[BSStore sharedStore]: unrecognized selector sent to class 0x69b8
2012-10-04 02:01:29.073 BarneyShop[1827:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[BSStore sharedStore]: unrecognized selector sent to class 0x69b8'
*** First throw call stack:

1 个答案:

答案 0 :(得分:2)

这是您的Singleton类应该是这样的:

#import "BSStore.h"

@implementation BSStore

@synthesize sharedNotebooks;

+ (BSStore *) sharedStore
{
    static BSStore * singleton;

    if ( ! singleton)
    {
        singleton = [[BSStore alloc] init];

    }
    return singleton;
}

@end

现在你可以致电:

[BSStore sharedStore].sharedNotebooks;