2012-04-30 13 views
1

上午沒有已知的類方法通過大書呆子牧場的iOS編程第三版我的工作,並已運行到與選擇「defaultStore」,這是一個單獨的問題。錯誤表示沒有這樣的類方法,我不知道如何解決我已經註釋過的問題。對於選擇「defaultStore」

我認爲,就是這本書將切換到核心數據的一部分,但我還沒有達到那一部分呢。

ItemsViewController.m

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

@implementation ItemsViewController 
- (id)init 
{ 
    // Call the superclass's designated initializer 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) { 
    UINavigationItem *n = [self navigationItem]; 

    [n setTitle:@"Homepwner"]; 

    // Create a new bar button item that will send 
    // addNewItem: to ItemsViewController 
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] 
          initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
          target:self 
          action:@selector(addNewItem:)]; 

    // Set this bar button item as the right item in the navigationItem 
    [[self navigationItem] setRightBarButtonItem:bbi]; 

    [[self navigationItem] setLeftBarButtonItem:[self editButtonItem]]; 
    } 
    return self; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [[self tableView] reloadData]; 
} 


- (IBAction)addNewItem:(id)sender 
{ 
    // Create a new BNRItem and add it to the store 
    BNRItem *newItem = [[BNRItemStore defaultStore] createItem];//No known class method for selector 'defaultStore' 
    // Incompatible pointer types initializing 'BNRItem*__strong' with an expression of 'NSArray' 

    // Figure out where that item is in the array 
    int lastRow = [[[BNRItemStore defaultStore] allItems] indexOfObject:newItem]; //No known class method for selector 'defaultStore' 

    NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0]; 

    // Insert this new row into the table. 
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] 
          withRowAnimation:UITableViewRowAnimationTop]; 
} 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    return [self init]; 
} 

- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
     toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    [[BNRItemStore defaultStore] moveItemAtIndex:[fromIndexPath row] //No known class method for selector 'defaultStore' 
             toIndex:[toIndexPath row]]; 
} 

- (void)tableView:(UITableView *)aTableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailViewController *detailViewController = [[DetailViewController alloc] init]; 

    NSArray *items = [[BNRItemStore defaultStore] allItems];//No known class method for selector 'defaultStore' 
    BNRItem *selectedItem = [items objectAtIndex:[indexPath row]]; 

    // Give detail view controller a pointer to the item object in row 
    [detailViewController setItem:selectedItem]; 

    // Push it onto the top of the navigation controller's stack 
    [[self navigationController] pushViewController:detailViewController 
             animated:YES]; 
} 

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // If the table view is asking to commit a delete command... 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
    BNRItemStore *ps = [BNRItemStore defaultStore];//No known class method for selector 'defaultStore' 
    NSArray *items = [ps allItems]; 
    BNRItem *p = [items objectAtIndex:[indexPath row]]; 
    [ps removeItem:p]; 

    // We also remove that row from the table view with an animation 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
        withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[BNRItemStore defaultStore] allItems] count];//No known class method for selector 'defaultStore' 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Create an instance of UITableViewCell, with default appearance 
    // Check for a reusable cell first, use that if it exists 
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    // If there is no reusable cell of this type, create a new one 
    if (!cell) { 
    cell = [[UITableViewCell alloc] 
      initWithStyle:UITableViewCellStyleDefault 
      reuseIdentifier:@"UITableViewCell"]; 
    } 
    // Set the text on the cell with the description of the item 
    // that is at the nth index of items, where n = row this cell 
    // will appear in on the tableview 
    BNRItem *p = [[[BNRItemStore defaultStore] allItems]//No known class method for selector 'defaultStore' 
       objectAtIndex:[indexPath row]]; 
    [[cell textLabel] setText:[p description]]; 
    return cell; 
} 
@end 

BNRItemStore.h

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


@implementation BNRItemStore 

+ (BNRItemStore *)defaultStore 
{ 
    static BNRItemStore *defaultStore = nil; 
    if(!defaultStore) 
     defaultStore = [[super allocWithZone:nil] init]; 

    return defaultStore; 
} 

+ (id)allocWithZone:(NSZone *)zone 
{ 
    return [self defaultStore]; 
} 

- (id)init 
{ 
    self = [super init]; 
    if(self) { 
     allItems = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

- (void)removeItem:(BNRItem *)p 
{ 
    [allItems removeObjectIdenticalTo:p]; 
} 

- (NSArray *)allItems 
{ 
    return allItems; 
} 

- (void)moveItemAtIndex:(int)from 
       toIndex:(int)to 
{ 
    if (from == to) { 
     return; 
    } 
    // Get pointer to object being moved so we can re-insert it 
    BNRItem *p = [allItems objectAtIndex:from]; 

    // Remove p from array 
    [allItems removeObjectAtIndex:from]; 

    // Insert p in array at new location 
    [allItems insertObject:p atIndex:to]; 
} 

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

    [allItems addObject:p]; 

    return p; 
} 

BNRItemStore.m

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


@implementation BNRItemStore 

+ (BNRItemStore *)defaultStore 
{ 
    static BNRItemStore *defaultStore = nil; 
    if(!defaultStore) 
     defaultStore = [[super allocWithZone:nil] init]; 

    return defaultStore; 
} 

+ (id)allocWithZone:(NSZone *)zone 
{ 
    return [self defaultStore]; 
} 

- (id)init 
{ 
    self = [super init]; 
    if(self) { 
     allItems = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

- (void)removeItem:(BNRItem *)p 
{ 
    [allItems removeObjectIdenticalTo:p]; 
} 

- (NSArray *)allItems 
{ 
    return allItems; 
} 

- (void)moveItemAtIndex:(int)from 
       toIndex:(int)to 
{ 
    if (from == to) { 
     return; 
    } 
    // Get pointer to object being moved so we can re-insert it 
    BNRItem *p = [allItems objectAtIndex:from]; 

    // Remove p from array 
    [allItems removeObjectAtIndex:from]; 

    // Insert p in array at new location 
    [allItems insertObject:p atIndex:to]; 
} 

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

    [allItems addObject:p]; 

    return p; 
} 
@end 
+0

你有'BNRItemStore'類的源代碼嗎? –

+1

編譯器查看'BNRItemStore'的'@ interface'塊,該塊可能位於頭文件BNRItemStore.h中,以確定該類具有哪些方法。 'BNRItemStore'是否聲明瞭一個名爲'defaultStore'的方法? –

+0

謝謝你提及。我忘了查看BNRItemStore.h,m文件。不知何故,我的版本沒有,只要我將它們複製到版本中,它就會被編譯。已經粘貼在BNRItemStore.h,.m文件中。再次感謝! – pdenlinger

回答

4

確認@interfaceBNRItemStore(在BNRItemStore.h)宣佈,方法:

@interface BNRItemStore : NSObject 

+ (BNRItemStore *)defaultStore; 

// etc. 

@end 

編譯器查看接口以瞭解該類上可用的方法。