ios - 使用单例获取 "unrecognized selector sent to class"

标签 ios

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

#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:

最佳答案

这是你的 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;

关于ios - 使用单例获取 "unrecognized selector sent to class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12718164/

相关文章:

ios - 如何在 AppDelegate 之外放置 FCM 的通知授权请求?

objective-c - UITableView 提交编辑错误

ios - 通过您自己的 iOS 应用程序将照片上传到 Instagram

ios - UIImagePickerControllerDelegate 未被调用

ios - UIActionSheet block View Controller dealloc

android - 同步联系人android和ios?

ios - Xcode 4.5 : The identity doesn't match any valid, 您的钥匙串(keychain)中未过期的证书/私钥对

ios - 必须分派(dispatch)到 iOS 11 中的主线程

iphone - 按下另一个按钮时执行当前 UITextField 自动更正建议

ios - 如果用户已经对应用进行了评分,如何避免向用户显示应用内评论提示