小编Gar*_*rry的帖子

为UIApplication委派财产

所以我开始学习iOS的核心数据并编写了这部分代码:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是这一行的目的是什么:

id delegate = [UIApplication sharedApplication].delegate;
Run Code Online (Sandbox Code Playgroud)

我知道,对于委托,您需要将特定对象的当前实例传递给delegate属性,该属性将该对象设置为另一个对象的委托.我的问题是你做的时候:

id delegate = [UIApplication sharedApplication].delegate;
Run Code Online (Sandbox Code Playgroud)

哪个对象被传递到该delegate属性?该sharedApplication方法返回我假设的应用程序的单例实例,然后您将获得delegate该应用程序的单个实例的属性.该委托是指在AppDelegate.h/ AppDelegate.mfiles中使用的那个委托吗?

如果该委托属性与AppDelegate.h …

delegates objective-c uiapplication uiapplicationdelegate ios

5
推荐指数
1
解决办法
509
查看次数