0

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
5
  • 2
    [self.tableView reloadData] doesn't call viewDidLoad. viewDidLoad is called only once, so that shouldn't be a problem Commented Jan 3, 2017 at 5:30
  • why does it go through its methods and creates another array then? @Vishnugondlekar Commented Jan 3, 2017 at 5:38
  • It would be better to use Singleton. See here: xcodenoobies.blogspot.my/2012/08/how-to-pass-data-between.html Commented Jan 3, 2017 at 5:40
  • @MarquaviousDraggon Check out Objective-C's literal syntax. It'll save you a lot of addObject: visual clutter: clang.llvm.org/docs/ObjectiveCLiterals.html Commented Jan 3, 2017 at 5:56
  • @MarquaviousDraggon set a breakpoint at the point the array gets created and you'll find out why that happens Commented Jan 3, 2017 at 6:33

2 Answers 2

0

You can take this array in AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>  {
}
@property (strong, nonatomic) NSMutableArray *arrayOfTodos;

And you can access it any where in project using this:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Fly Back to SF" todoDeadline:@"8/6/17" todoStatus:inProgress]];

Would be better if you create a Global class for all such global data and function.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't think I should create variables in the app delegate. How do I replicate the swift version? In the swift version, I could just make an array of objects and use it in the View Controller. is there not an Objective - C equivalent?
See first You have to decide whether you want to access it with using MainVC object or Some Global class object?
if you want to access it using MainVC object so u need to take it in Appdelagate. As you have already created a property of @property (strong, nonatomic) NSMutableArray *arrayOfTodos; so you can access in every where in project but using MainVC object only MainVC *obj; obj.arrayOfTodos like this. First you clear what you want i will provide you solution with code
What is the issue @iOS Developer
her is the link of my new question stackoverflow.com/questions/49773206/…
0

As you have explained in your above question by

The line below adds to the the _arrayOfTodos.

[_arrayOfTodos addObject: [[Todo alloc]initWithInformation:@"Fly Back to SF" todoDeadline:@"8/6/17" todoStatus:inProgress]];

You will defiantly get the initial array. Because you initialize your array by initializing the TODO. Rather you can make a comen object of your TODO once when you initializing your array object and using that you can add your object to your array, some what like:

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrayOfTodos = [[NSMutableArray alloc]init];
    _todo = [[Todo alloc] init];
    [_arrayOfTodos addObject: [_todo initWithInformation:@"Fly Back to SF" todoDeadline:@"8/6/17" todoStatus:inProgress]];
    [_arrayOfTodos addObject: [_todo initWithInformation:@"Wash Clothes" todoDeadline:@"4/11/17" todoStatus:inProgress]];
    [_arrayOfTodos addObject: [_todo initWithInformation:@"Read Books" todoDeadline:@"5/11/17" todoStatus:unfinished]];
    [_arrayOfTodos addObject: [_todo initWithInformation:@"Cook Pasta" todoDeadline:@"3/9/17" todoStatus:finished]];
    [_arrayOfTodos addObject: [_todo initWithInformation:@"Feed Dog" todoDeadline:@"1/1/17" todoStatus:finished]];
}

Above _todo is a globally declared object which you can declare with array object.
When you use this globally declared object of TODO, your array will not re-initialize every time you add a new object.
Hope this may help you and may solve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.