我已经完成了几次XCode应用程序开发教程并不断提出同样的错误。
source.ToDoItem给出错误“no getter method to read from property”
有关如何解决此问题的任何想法?我似乎无法编写正确的getter方法,但我无法在开发教程中找到一个示例。
- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
AddToDoItemViewController *source = [segue sourceViewController];
ToDoItem *item = source.ToDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
ToDoItem.h
#import <Foundation/Foundation.h>
@interface ToDoItem : NSObject
@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
@end
ToDoItem.m
#import "ToDoItem.h"
@implementation ToDoItem
@end
ToDoListTableViewController
#import "ToDoListTableViewController.h"
#import "AddToDoItemViewController.h"
#import "ToDoItem.h"
@interface ToDoListTableViewController ()
@property NSMutableArray *toDoItems;
@end
@implementation ToDoListTableViewController
- (void)loadInitialData {
ToDoItem *item1 = [[ToDoItem alloc] init];
item1.itemName = @"things";
[self.toDoItems addObject:item1];
ToDoItem *item2 = [[ToDoItem alloc] init];
item2.itemName = @"Things to do with things";
[self.toDoItems addObject:item2];
ToDoItem *item3 = [[ToDoItem alloc] init];
item3.itemName = @"Write Ethics Paper";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
AddToDoItemViewController *source = [segue sourceViewController];
ToDoItem *item = source.ToDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// 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 {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
// Configure the cell...
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
@end
和ToDoListTableViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"
#import "AddToDoItemViewController.h"
@interface ToDoListTableViewController : UITableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
@end
AddToDoItemViewController.m
#import "AddToDoItemViewController.h"
#import "ToDoItem.h"
@interface AddToDoItemViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@end
@implementation AddToDoItemViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if (sender != self.saveButton) return;
if (self.textField.text.length > 0) {
}
self.toDoItem = [[ToDoItem alloc] init];
self.toDoItem.itemName = self.textField.text;
self.toDoItem.completed = NO;
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
@end
AddToDoItemViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"
@interface AddToDoItemViewController : UIViewController
@property ToDoItem *toDoItem;
@property NSString *itemName;
@property BOOL completed;
@property (readonly) NSDate *creationDate;
@end
答案 0 :(得分:0)
尝试更改这两行:
@property NSString *itemName;
@property BOOL completed;
为此:
@property (nonatomic, strong) NSString *itemName;
@property (nonatomic, readwrite) BOOL completed;
答案 1 :(得分:0)
在AddToDoItemViewController
你有
@property ToDoItem *toDoItem;
但在代码中您将其用作:
ToDoItem *item = source.ToDoItem;
应该是:
ToDoItem *item = source.toDoItem;
请注意命名约定。类名以大写字母开头,属性名称以小写字母开头。