使用 MGTwitterEngine 单例
我读到了更改 MGTwitterEngine 的委托,但并没有真正理解它。我希望有人能再次解释一下。
根据我所知,我为 MGTwitterEngine 创建一个包装器,并在包装器中设置委托。因此,为了变得更容易,我尝试为接口提供一个 NSArray 实例,每当需要时我都会传递它。
下面是接收到的状态的代码:
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{
//NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);
[statusIds setObject:statuses forKey:connectionIdentifier];
}
所以我希望项目中的任何对象都可以访问sharedTwitterEngine,只要我先请求信息,然后释放statusContainer,使用新的结果并将其传递给我的工作对象以供稍后使用使用。
我不确定这是否是正确的方法,或者是否有我错过的更简单的方法?
I read about Change the delegate of MGTwitterEngine and don't really get it. I hope someone can explain it again.
Based on what I know, I create a wrapper for MGTwitterEngine and setup the delegate within the wrapper. So in order to make easier, I try to have an NSArray instance for the interface which I'll pass around whenever I need it.
Here is the code for the status received:
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
{
//NSLog(@"Got statuses for %@:\r%@", connectionIdentifier, statuses);
[statusIds setObject:statuses forKey:connectionIdentifier];
}
So I expect the sharedTwitterEngine can be accessed by any object within the project, as long I request for information first, and release the statusContainer, using the new result and pass it to my working object for later use.
I'm not sure if it is the correct way, or are there any easier ways that I missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您链接的 SO 帖子提出的解决方案可以通过以下方式实现:
1)创建 MGTwitterEngine 的包装器;这个包装器将公开 MGTwitterEngine 所需的任何选择器,并向每个选择器添加一个参数,该参数标识正在调用它的视图控制器;
2) MGTwitterEngine 的包装器将充当所有发送请求的唯一委托;
3) 对于包装器从视图控制器接收到的每个请求,包装器会将视图控制器地址存储在与 twitter id 关联的 NSMutableDictionary 中;
4) 当响应返回时,委托(与包装器是同一对象)将找出最初发送请求的视图控制器(通过在字典中搜索响应附带的 twitter id),并转发对此作出回应。
我希望这会有所帮助......
编辑:
这就是你可以做到的(我只包括 1 个 API 调用和相关代码):
The solution proposed by the S.O. post you link could be implemented in this way:
1) create a wrapper to MGTwitterEngine; this wrapper will expose any selector you need of MGTwitterEngine and will add to each of them a parameter which identifies the view controller that is calling it;
2) your wrapper to MGTwitterEngine would act as a unique delegate to all requests sent;
3) for each request that the wrapper receives from a view controller, the wrapper will store the view controller address in a NSMutableDictionary associated to the twitter id;
4) when a response comes back, the delegate (which is the same object as the wrapper) will find out which view controller sent the request initially (by searching in the dictionary for the twitter id that came with the response), and forward the response to it.
I hope this helps....
EDIT:
this is how you could do it (I am including only 1 API call and just the relevant code):