这两个预定方法调用之间的区别 - iOS

The difference between these two scheduled method calls - iOS

我已经看过几次了,但似乎无法找到两者之间的区别...

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(loginViewFetchedUserInfo:)
                                             name:FBSDKProfileDidChangeNotification
                                           object:nil];

- (void)loginViewFetchedUserInfo:(NSNotification *)notification

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(loginViewFetchedUserInfo)
                                             name:FBSDKProfileDidChangeNotification
                                           object:nil];

- (void)loginViewFetchedUserInfo

我知道 (void)methodname:(TYPE *)newName 可以将值传递给该方法,但我不知道上面两个有什么区别以及为什么你会做第一个(用于 Facebook SDK 示例)超过第二个。

第一个方法将 NSNotification 对象传递给该方法。通过这种方式,您可以访问有关通知的信息。

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(loginViewFetchedUserInfo:)
                                         name:FBSDKProfileDidChangeNotification
                                       nil];

例如,如果通知是使用 userInfo 字典发布的

NSDictionary *userInfo = @{@"Blah" : @"foo"};
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKProfileDidChangeNotification object:self userInfo:userInfo];

并且您想在方法中访问 userInfo。您还可以访问通知的发件人,即通知的 object.

- (void)loginViewFetchedUserInfo:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSObject *sender = notification.object;
}