我正在查看Apple Start Developing Guide,在演示应用程序中遇到了一个错误。
代码如下:
#import "XYZToDoListViewController.h"
#import "XYZToDoItem.h"
@interface XYZToDoListViewController ()
@property NSMutableArray *toDoItems;
-(void)viewDidLoad{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc]init];
[self loadInitialData];
}
@end
@implementation XYZToDoListViewController
-(void)loadInitialData{
XYZToDoItem *item1 = [[XYZToDoItem alloc]init];
item1.itemName = @"Buy Milk";
[self.toDoItems addObject:item1];
XYZToDoItem *item2 = [[XYZToDoItem alloc]init];
item2.itemName = @"Go Shopping";
[self.toDoItems addObject:item2];
XYZToDoItem *item3 = [[XYZToDoItem alloc]init];
item3.itemName = @"Wake Up";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end在构建时,它会出现一个错误,说它应该在viewDidLoad之后使用;。我已经在谷歌上搜索过了,看看我是否能找出原因,但似乎不能。
发布于 2013-11-15 22:34:08
将其放入您的.h文件中
@interface className : Class that you inherit
@property NSMutableArray *toDoItems;
@end当然,您需要将className替换为您的类名,并将“继承的类”替换为超类,如NSObject、UIViewController、UIView或任何其他类。
并将其放入您的.m文件中
@implementation className
-(void)viewDidLoad{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc]init];
[self loadInitialData];
}
@end这应该会有帮助。
希望它能有所帮助:)
发布于 2013-11-15 22:31:29
看起来你可能有这样的属性:
@property NSMutableArray *toDoItems;在实现文件(.m)或方法中:
-(void)viewDidLoad{
...
}在头文件(.h)中。
此外,您还需要一个@implementation或@interface。也许你应该用文件中的完整代码来更新你的问题。
发布于 2013-11-15 22:50:46
下面的属性应该在.h文件中。因为在.h文件中我们需要声明方法,所以属性将声明应该在.h文件中的方法
@property NSMutableArray *toDoItems;下面将在.m文件中,因为在.m文件中我们需要定义方法。
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
}https://stackoverflow.com/questions/20003300
复制相似问题