I am creating a simple ToDo application to learn the Objective-C language. The initial view is a Tableview Controller and the cells display the diffrent Todo's.
Here is my code in the Swift Language:
var toDos:[ToDo]=[
ToDo(taskTitle: "Feed the dog", taskDeadline: "11/23/2016", finishedState: .finished),
ToDo(taskTitle: "Eat Food", taskDeadline: "12/23/2016", finishedState: .notFinished),
ToDo(taskTitle: "Clean Up Room", taskDeadline: "10/13/2016", finishedState: .finished)
]
// MARK: ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
}
Here is my code for ObJective-C
- (void)viewDidLoad {
[super viewDidLoad];
_arrayOfTodos = [[NSMutableArray alloc]init];
[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Fly Back to SF" todoDeadline:@"8/6/17" todoStatus:inProgress]];
[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Wash Clothes" todoDeadline:@"4/11/17" todoStatus:inProgress]];
[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Read Books" todoDeadline:@"5/11/17" todoStatus:unfinished]];
[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Cook Pasta" todoDeadline:@"3/9/17" todoStatus:finished]];
[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Feed Dog" todoDeadline:@"1/1/17" todoStatus:finished]];
}
Notice how in the swift version, the array is created above the viewDidLoad.
This allows me to use that array throughout that View Controller.
Notice in the Objective-C version, the array is created and populated in the viewDidLoad. This poses as a problem because when I add a new Todo object, and then call the [self.tableView reloadData]; to update the view, it calls the viewDidLoad method and it re-inits the array. As a result, its like I never added the new ToDo object in the first place (Because the array is re-initialized and re-populated by the Todo object initializers (See how I add to the array below).
The line below adds to the the _arrayOfTodos.
[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Fly Back to SF" todoDeadline:@"8/6/17" todoStatus:inProgress]];
I would like to know how to create an array of ToDo objects ABOVE the view did load (or someplace else I can use it globally in the Controller).
Here is my MainVC.h
#import <UIKit/UIKit.h>
#import "Todo.h"
@class Todo;
@interface MainVC : UITableViewController
-(void)addNewTodo:(Todo *)todo;
@property (strong, nonatomic) NSMutableArray *arrayOfTodos;
@end
[self.tableView reloadData]doesn't callviewDidLoad.viewDidLoadis called only once, so that shouldn't be a problemaddObject:visual clutter: clang.llvm.org/docs/ObjectiveCLiterals.html