如何将 AsyncSocket 实例声明为全局
我正在将 AsyncSocket 类用于聊天应用程序。但我想使用在整个项目的登录页面中创建的 AsyncSocket 实例。这意味着我想重用在 chatViewControl 类的登录页面中创建的 AsyncSocket 实例。 谁能帮我找到解决方案?
I am using AsyncSocket class for a chat application. But i want to use the AsyncSocket instance creating in the Login page for the entire project. That means i want to reuse the instance of AsyncSocket which created in the Login page for the chatViewControl Class.
Can anyone help me to find a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望在应用程序范围内引用单个
AsyncSocket
,您可以考虑将其声明为 应用程序委托。您可以按照以下方式进行操作:从现在起,您可以通过简单地执行以下操作来访问您的套接字:
If you wish to have an application-wide reference to a single
AsyncSocket
, you could consider declaring it as a property of your application delegate. You could do it along the following lines:From this point on you can access your socket by simply doing:
您确实应该尽可能避免全局状态(请参阅 Sven 的链接)。一般来说,您可能最好将某种对象传递给其他对象。
例如,我正在开发的当前应用程序有一个 ServerInfo 对象,该对象是我在登录之前创建的,并在创建它时传递给每个新的视图控制器,它包含套接字、加密信息、当前消息 ID、用户名等 但是,
要回答如何将其声明为全局的问题,您可以像在 C: 中那样在
login.h: 中
在 login.m: 中进行操作,
然后在创建套接字时
以及在中使用它时进行操作。其他文件:
You should really avoid global state where possible (see Sven's link). In general you're probably better of having some kind of object that you pass around to your other objects.
For example, the current app I'm working on I have a ServerInfo object that I create before logging in and pass to each new view controller when I create it, and it contains the socket, encryption information, current message id, username, etc.
However, to answer the question of how to declare it as a global, you can do it just like in C:
in login.h:
in login.m:
then when you create the socket:
and when you want to use it in other files: