Delegate Property for UIApplication

So, I'm starting to learn Core Data for iOS and wrote this section of code:

- (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 that for a delegate, you need to pass the current instance of a specific object to the delegate property, which sets that object as a delegate to another object. My question is here when you do:

 id delegate = [UIApplication sharedApplication].delegate; 

what object is passed to this delegate property? The sharedApplication method returns one application instance that I accept, and then you get the delegate property for that single application instance. AppDelegate.h this delegate refer to the one used in the AppDelegate.h / AppDelegate.m ?

And if this delegation property is the same as that used in the AppDelegate.h / AppDelegate.m , there should not be a line of code in the AppDelegate.m file in the AppDelegate.m method, where we will go through the current instance of AppDelegate.m in the delegate property, similar to what we do for tableView :

 self.tableView.delegate = self. 

I do not see where this is done for the delegate property on this line:

 id delegate = [UIApplication sharedApplication].delegate; 
+5
source share
1 answer

You do not need to designate AppDelegate as a delegate for your application, because it automatically runs for you.

In objective-c, you can see the main.m function, where you can find the AppDelegate class mention of you:

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

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

There is no main.m in Swift, so you always have @UIApplicationMain before declaring your delegate class:

 @UIApplicationMain final class AppDelegate: UIResponder, UIApplicationDelegate { } 

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

So, if you change a property in your AppDelegate from one view controller, you can be sure that other controllers will provide access to the modified version. You can easily verify this by running a simple counter and checking that you can always read the last value :)

+4
source

All Articles