2012-04-30 68 views
1

在我註釋掉的.m文件中,我收到了兩個與'NSArray類型的表達式不兼容的指針類型'的警告。與NSArray類型的表達式不兼容的指針類型

不完全明白這一點,不知道如何解決這個問題。你能解釋一下,以便我可以修復它嗎?

在此先感謝。

ItemsViewController.m

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

@implementation ItemsViewController // Incomplete implementation 

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

    NSArray *items = [[BNRItemStore sharedStore]allItems]; 
    BNRItem *selectedItem = [items objectAtIndex:[indexPath row]]; 

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

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

-(id)init 
{ 
    // Call the superclass's designated initializer 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) { 


    } 
    return self; 
} 

-(id)initWithStyle:(UITableViewStyle)style 
{ 
    return [self init]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[BNRItemStore sharedStore]allItems]count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    // 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 sharedStore]allItems]objectAtIndex:[indexPath row]]; 

    [[cell textLabel]setText:[p description]]; 

    return cell; 
} 

-(UIView *)headerView 
{ 
    // If we haven't loaded the headerView yet 
    if (!headerView) { 
    //Load HeaderView.xib 
    [[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil]; 
    } 
    return headerView; 
} 

-(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec 
{ 
    return [self headerView]; 
} 

-(CGFloat)tableView:(UITableView *)tv heightForHeaderInSection:(NSInteger)sec 
{ 
    // The height of the header view should be determined from the height of the 
    // view in the XIB file 
    return [[self headerView]bounds].size.height; 
} 

-(IBAction)toggleEditingMode:(id)sender 
{ 
    // If we are currently in editing mode 
    if ([self isEditing]) { 
    // Change text of button to inform user of state 
    [sender setTitle:@"Edit" forState:UIControlStateNormal]; 
    // Turn off editing mode 
    [self setEditing:NO animated:YES]; 
    } else { 
    // Change text of button to inform user of state 
    [sender setTitle:@"Done" forState:UIControlStateNormal]; 
    // Enter editing mode 
    [self setEditing:YES animated:YES]; 
    } 
} 

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

    // Figure out where that item is in the array 
    int lastRow = [[[BNRItemStore sharedStore]allItems]indexOfObject:newItem]; 

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

    // Insert this new row into the table 
    [[self tableView]insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop]; 
} 

-(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 sharedStore]; 
    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]; 
    } 
} 

-(void)tableView:(UITableView *)tableView 
    moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
     toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 
    [[BNRItemStore sharedStore]moveItemAtIndex:[sourceIndexPath row] 
            toIndex:[destinationIndexPath row]]; 
} 

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


@end 

ItemsViewController.h

#import <Foundation/Foundation.h> 
#import "DetailViewController.h" 

@interface ItemsViewController : UITableViewController 
{ 
    IBOutlet UIView *headerView; 
} 

-(UIView *)headerView; 
-(IBAction)addNewItem:(id)sender; 
-(IBAction)toggleEditingMode:(id)sender; 

@end 

BNRItemStore.m

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

@implementation BNRItemStore 

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

    return sharedStore; 
} 

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

    } 
    return self; 
} 

-(NSArray *)allItems 
{ 
    return allItems; 
} 

-(NSArray *)createItem 
{ 
    BNRItem *p = [BNRItem randomItem]; 
    [allItems addObject:p]; 
    return p; //Incompatible pointer types returning 'BNRItem*__strong' from a function with result type 'NSArray*' 
} 

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

-(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]; 
} 

@end 

BNRItemStore.h

#import <Foundation/Foundation.h> 

@class BNRItem; 

@interface BNRItemStore : NSObject 
{ 
    NSMutableArray *allItems; 
} 

// Notice that this is a class method and prefixed with a + instead of a - 
+(BNRItemStore *)sharedStore; 

-(void)removeItem:(BNRItem *)p; 
-(void)moveItemAtIndex:(int)from 
       toIndex:(int)to; 

-(NSArray *)allItems; 
-(NSArray *)createItem; 

@end 

回答

2

查看方法定義的返回類型,然後查看要返回的var的類型。

-(NSArray *)createItem 
{ 
    BNRItem *p = [BNRItem randomItem]; 
    [allItems addObject:p]; 
    return p; //Incompatible pointer types returning 'BNRItem*__strong' from a function with result type 'NSArray*' 
} 

好像你應該返回allItems代替p

或者將退貨類型更改爲BNRItem *

2

在這裏:

-(NSArray *)createItem 
{ 
    BNRItem *p = [BNRItem randomItem]; 
    [allItems addObject:p]; 
    return p; //Incompatible pointer types returning 'BNRItem*__strong' from a function with result type 'NSArray*' 
} 

pBNRItem*類型的變量。您的方法聲稱其返回值爲NSArray*警告是因爲這些不同。如果要返回整個數組,請返回allItems

如果要返回新項目,請更改返回類型。

+0

是的,這有效,謝謝。但我仍然與其他方法混淆,因爲我想添加一個新的商品到商店,這是一個單身數組和一個容器。我需要給BNRItemStore一個弱關係,而BNRItemStore與BNRItem有很強的關係嗎?如果是這樣,我該怎麼做?謝謝。 – pdenlinger

2
-(void)moveItemAtIndex:(int)from 
       toIndex:(int)to 

該方法將按書面形式工作;如果tofrom之後,則會將該對象移動到錯誤的索引處,並可能導致該過程中出現超出範圍的異常。


正如其他人所說,您的 createItem方法被宣佈爲返回 NSArray*,但您返回 BNRItem*

修復返回類型也將修復其他警告。


在對「quixoto」的評論中,您提出了一些建模問題。聽起來你應該真的在使用CoreData,它明確地設計用於完成這種類型的建模任務。

+3

OP正在完成一本教程書,並沒有進入CoreData章節。 –

相關問題