1

在我已注释掉的 .m 文件中,我收到了两个关于“与类型为 NSArray 的表达式不兼容的指针类型”的警告。

不完全理解这一点,也不知道如何解决这个问题。你能解释一下,以便我修复它吗?

提前致谢。

项目视图控制器.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
4

3 回答 3

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*'
}

p是一个类型的变量BNRItem*。您的方法声称其返回值是NSArray*警告是因为这些是不同的。

如果要返回整个数组,请返回allItems

如果要退回新商品,请更改退货类型。

于 2012-04-30T17:09:49.147 回答
2
-(void)moveItemAtIndex:(int)from 
               toIndex:(int)to

该方法将按书面方式工作;如果to是 after from,您会将对象移动到错误的索引,并且可能会在此过程中导致超出范围的异常。


正如其他人所说,您的createItem方法被声明为返回 anNSArray*但您返回的是 a BNRItem*

修复返回类型也将修复其他警告。


在您对“quixoto”的评论中,您提出了一些建模问题。听起来您真的应该使用 CoreData,它被明确地设计为允许进行此类建模任务。

于 2012-04-30T17:47:02.580 回答
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 *.

于 2012-04-30T17:08:18.150 回答