5

So I am starting to learn Core Data for iOS and have wrote this section of code out:

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    id delegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *objectContext = [delegate managedObjectContext];

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Album"];
    //An array of our sorted Album objects by date in ascending order
    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]];

    NSError *error = nil;

    NSArray *fetchedAlbums = [objectContext executeFetchRequest:fetchRequest error:&error];

    self.albums = [fetchedAlbums mutableCopy];

    [self.tableView reloadData];
}

My question is what is the purpose of this line:

id delegate = [UIApplication sharedApplication].delegate;

I know for a fact that for a delegate you need to pass in the current instance of a specific object to the delegate property which sets that object as the delegate for the other object. My question here is when you do:

id delegate = [UIApplication sharedApplication].delegate;

which object is being passed in to that delegate property? The sharedApplication method returns a singleton instance of the app I am assuming and then you are getting the delegate property for that single instance of the app. Is that delegate referring to the same one being used in the AppDelegate.h/AppDelegate.m files?

And if that delegate property is the same one that is being used in AppDelegate.h/AppDelegate.m files shouldn't there be a line of code in the AppDelegate.m file in the viewDidLoad method where we be pass in the current instance of the AppDelegate.m in the delegate property, similar to what we do for a tableView:

self.tableView.delegate = self. 

I don't see where that is done for the delegate property in this line:

id delegate = [UIApplication sharedApplication].delegate;
juliocesar
  • 5,706
  • 8
  • 44
  • 63

1 Answers1

4

You do not need to assign your AppDelegate as a delegate for your application because it is being automatically done for you.

In objective-c you can look at main.m function where you can find the mentioning of you AppDelegate class:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        return retVal;
    }
}

After this call the system will set the instance of class AppDelegate as a delegate for your application.

In Swift, main.m is missing which is why you always have @UIApplicationMain before the declaration of your delegate class:

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {
}

Because UIApplication.sharedApplication() is a singleton, UIApplication.sharedApplication().delegate always returns the same instance of AppDelegate.

So, if you modify some property in your AppDelegate from one view controller you can be sure that the other view controllers will have access to the modified version. You can easily verify it yourself by implementing a simple counter and checking that you always can read the latest value :)

Andriy Gordiychuk
  • 6,163
  • 1
  • 24
  • 59
  • OHH, amazing explanation Andriy... Thank you so much! Regarding your post I have one more quick question. I noticed that the method: int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate"); takes four arguments. Why is the third argument set to nil? I understand that the last argument is basically saying which class should the delegate be set to, but what does the third argument specify? – Garry Harry Aug 23 '15 at 23:45
  • 1
    @GarryHarry third argument is the PrincipalClass name. Passing nil defaults to UIApplication :) You can read more here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/ . As to the function - you have main.m in your project as well. Search for it in the file inspector – Andriy Gordiychuk Aug 23 '15 at 23:50
  • Hey Andriy! Thanks for the quick response. I just looked at the class reference and it made much more sense now. Also sorry if I am asking too much, but what are the values argc and argv parameters that are passing in the UIApplicationMain function? I have coded in C in college, and when you run your .c file in terminal using gcc you specify your argv by passing in the files on terminal and then in your .c file you can do something like check if (argc == somenumber) then do something? Is there any way you can figure out what is being passed in to the argv? – Garry Harry Aug 24 '15 at 00:01
  • 1
    @GarryHarry as you see, it passes through the variables which it gets when the function is called by the system. I think you might be able to log them with NSLog :) – Andriy Gordiychuk Aug 24 '15 at 00:02
  • Oh arite, I will try that! Thanks for the suggestions... helped me out a lot! – Garry Harry Aug 24 '15 at 00:03