我有一个 NSObject,它是一个单例。这个单例类有一个委托(delegate)有什么问题吗?我担心单例类型会失败。
这是我的场景。我有一个函数(在这个单例类中)执行异步请求以从 API 中提取 NSDictionary。基本上当这个请求完成时,我想通知一个类请求已经完成。
最佳答案
不,委托(delegate)不会失败,但考虑使用 NSNotificationCenter相反:
static NSString *const kMyClassNotificationName = @"myClassNotificationName";
// where you would call a delegate method (e.g. [self.delegate doSomething])
[[NSNotificationCenter defaultCenter] postNotificationName:kMyClassNotificationName object:self userInfo: /* dictionary containing variables to pass to the delegate */];
// where you would set up a delegate (e.g. [Singleton instance].delegate = self)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:kMyClassNotificationName object:[Singleton instance]];
关于objective-c - 委托(delegate)单例对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475469/