我的.m文件中有两个警告“我的.m文件中包含类型为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
答案 0 :(得分: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 *
。
答案 1 :(得分: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
。
如果要返回新项目,请更改返回类型。
答案 2 :(得分:2)
-(void)moveItemAtIndex:(int)from
toIndex:(int)to
该方法将按照书面形式工作;如果to
在from
之后,您将把对象移动到错误的索引,并可能导致进程中出现超出范围的异常。
createItem
方法被声明为返回NSArray*
但您返回的是BNRItem*
。
修复返回类型也会修复其他警告。
<小时/> 在你对“quixoto”的评论中,你会问一些建模问题。听起来你真的应该使用CoreData,这是明确设计的,以便完全允许这种类型的建模任务。