Objective c 对于';没有可见的@界面;BNRItemStore';声明选择器';福基';

Objective c 对于';没有可见的@界面;BNRItemStore';声明选择器';福基';,objective-c,Objective C,我是objective C的新手,一直在学习《IOS编程大书呆子牧场指南第四版》。我经常遇到一个反复出现的错误,在过去的几个小时里我一直在努力解决它。经过一段时间的研究,我来到了这里。任何帮助都将不胜感激 “没有可见的@interface for'BNRItemStore'声明选择器'deleteImageForKey;'” 贝尼特斯酒店 #import <Foundation/Foundation.h> @class BNRItem; @interface BNRItemSto

我是objective C的新手,一直在学习《IOS编程大书呆子牧场指南第四版》。我经常遇到一个反复出现的错误,在过去的几个小时里我一直在努力解决它。经过一段时间的研究,我来到了这里。任何帮助都将不胜感激

“没有可见的@interface for'BNRItemStore'声明选择器'deleteImageForKey;'”

贝尼特斯酒店

#import <Foundation/Foundation.h>

@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic, readonly) NSArray *allItems;

// Notice that this is a class method and prefixed with a + instead of a -
+ (instancetype)sharedStore;
- (BNRItem *)createItem;
- (void)removeItem:(BNRItem *)item;

- (void)moveItemAtIndex:(NSInteger)fromIndex
                toIndex:(NSInteger)toIndex;

@end
利马百货公司

#import <Foundation/Foundation.h>

@interface BNRImageStore : NSObject

+ (instancetype)sharedStore;

- (void)setImage:(UIImage *)image forKey:(NSString *)key;
- (UIImage *)imageForKey:(NSString *)key;
- (void)deleteImageForKey:(NSString *)key;

@end

您正在BNRImageStore类中声明方法deleteImageForKey,而不是在BNRItemStore中

检查您在BNRItemStore.m中removeItem的实现

#import "BNRItemStore.h"
#import "BNRItem.h"


@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;

@end

@implementation BNRItemStore

+ (instancetype)sharedStore
{
    static BNRItemStore *sharedStore = nil;

    // Do I need to create a sharedStore?
    if (!sharedStore) {
    sharedStore = [[super allocWithZone:nil] init];
}

    return sharedStore;
}

// If a programmer calls [[BNRItemStore alloc] init], let him
// know the error of his ways
- (instancetype)init
{
    @throw [NSException exceptionWithName:@"Singleton"
                                   reason:@"Use +[BNRItemStore sharedStore]"
                                 userInfo:nil];
    return nil;
}

// Here is the real (secret) initializer 
- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        _privateItems = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSArray *)allItems
{
    return [self.privateItems copy];
}

- (BNRItem *)createItem
{
    BNRItem *item = [BNRItem randomItem];

    [self.privateItems addObject:item];

    return item;
}

- (void)removeItem:(BNRItem *)item
{
    NSString *key = item.itemKey;
    if (key) {
        [[BNRItemStore sharedStore] deleteImageForKey:key];
    }

    [self.privateItems removeObjectIdenticalTo:item];
}

- (void)moveItemAtIndex:(NSInteger)fromIndex
                toIndex:(NSInteger)toIndex
{
    if (fromIndex == toIndex) {
         return;
    }
    // Get pointer to object being moved so you can re-insert it
    BNRItem *item = self.privateItems[fromIndex];

    // Remove item from array
    [self.privateItems removeObjectAtIndex:fromIndex];

    // Insert item in array at new location
    [self.privateItems insertObject:item atIndex:toIndex];
}

@end
- (void)removeItem:(BNRItem *)item
{
    NSString *key = item.itemKey;
    if (key) {
        [[BNRItemStore sharedStore] deleteImageForKey:key];
    }

    [self.privateItems removeObjectIdenticalTo:item];
}
我想你指的是“bRimageStore”,而不是bRitemStore


除了输入错误,您还应该了解Objective-C对象对选择器的响应。每个Objective-C对象都有一个它响应的选择器数组。当您看到错误:“对于'BNRItemStore'没有可见的@interface声明选择器'deleteImageForKey;'”您应该理解编译器没有看到您指定的选择器被错误中的类理解

您在哪里调用此方法并导入相应的头文件?您在BNRItemStore上调用此方法,但它在BNRImageStore中声明…不同的类。此外,需要导入BNRImageStore.h。你可能会发现在上的论坛对回答大书呆子书中的代码非常有帮助。我建议将你的评论编辑到你的答案中,而不是在一个不可编辑的页面中扩展(还要注意,询问者没有得到通知,尽管我是,因为我是这里唯一的评论者)。我已经实现了这些更改,现在收到两个单独的错误:“选择器'sharedStore'没有已知的类方法”和“选择器'deleteImageForKey'没有已知的实例方法”。此时,编译器不知道BNRItemStore如何实现什么类或实例方法。我们需要查看所有代码才能理解,但这很可能与未包含接口或错误键入类名有关。我再次鼓励您前往Big Nerd Ranch论坛,在那里您可以发布所有代码。你会发现其他人也在经历同样的挑战,而对于其他用户来说,眼前的问题很可能是非常新鲜的。
- (void)removeItem:(BNRItem *)item
{
    NSString *key = item.itemKey;
    if (key) {
        [[BNRItemStore sharedStore] deleteImageForKey:key];
    }

    [self.privateItems removeObjectIdenticalTo:item];
}