2014-04-14 51 views
0

我是新來的目標C,並已與書「IOS編程大書呆子牧場指南第4版」學習。我不斷收到一個重複的錯誤,我一直在努力解決它在過去的幾個小時。經過一番研究,我來到了這裏。任何幫助將不勝感激。'BNRItemStore'沒有可見的@interface聲明選擇器'deleteImageForKey;'

「爲 'BNRItemStore' 不可見@interface聲明選擇 'deleteImageForKey;'」

BNRItemStore.h

#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 

BNRItemStore.m

#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 

BNRImageStore.h

#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.m

#import "BNRImageStore.h" 

@interface BNRImageStore() 

@property (nonatomic, strong) NSMutableDictionary *dictionary; 

@end 

@implementation BNRImageStore 

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

    if (!sharedStore) { 
     sharedStore = [[self alloc] initPrivate]; 
} 

    return sharedStore; 
} 

// No one should call init 
- (instancetype)init 
{ 
    @throw [NSException exceptionWithName:@"Singleton" 
            reason:@"Use +[BNRImageStore sharedStore]" 
           userInfo:nil]; 
    return nil; 
} 

// Secret designated initializer 
- (instancetype)initPrivate 
{ 
    self = [super init]; 

    if (self) { 
     _dictionary = [[NSMutableDictionary alloc] init]; 
    } 

    return self; 
} 

- (void)setImage:(UIImage *)image forKey:(NSString *)key 
{ 
    self.dictionary[key] = image; 
} 

- (UIImage *)imageForKey:(NSString *)key 
{ 
    return self.dictionary[key]; 
} 

- (void)deleteImageForKey:(NSString *)key 
    { 
     if (!key) { 
     return; 
     } 
     [self.dictionary removeObjectForKey:key]; 
    } 

    @end 
+0

哪裏可以調用這個方法,並且導入適當的頭文件? –

+1

您正在BNRItemStore上調用方法,但它在BNRImageStore ...不同的類中聲明。 –

回答

2

你宣佈你BNRImageStore類中的方法deleteImageForKey,而不是在你的BNRItemStore。

檢查您的removeItem的實現:在BNRItemStore.m

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

    [self.privateItems removeObjectIdenticalTo:item]; 
} 

我假設你的意思是指 「BNRImageStore」,而不是BNRItemStore。

除了錯字,你應該明白,Objective-C對象響應選擇器。每個Objective-C對象都有一個響應的選擇器數組。當你看到錯誤:「'BNRItemStore'沒有可見的@interface'聲明選擇器'deleteImageForKey;'」你應該明白編譯器沒有看到你指定的選擇器在類中被錯誤理解。

+1

另外,需要導入BNRImageStore.h。 –

+0

你可能會發現http://forums.bignerdranch.com上的論壇對於大書呆子書中提供的代碼的答案非常有幫助。 – blackirishman

+0

我建議編輯你的意見到你的答案,而不是在一個不可編輯和[通常無常的媒體](http://meta.stackexchange.com/a/19757/)擴大(也注意提問者沒有得到通知雖然我是因爲我是這裏唯一的評論者)。 –

相關問題