如何将 AsyncSocket 实例声明为全局

发布于 2024-09-24 00:25:24 字数 133 浏览 6 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-10-01 00:25:24

如果您希望在应用程序范围内引用单个 AsyncSocket,您可以考虑将其声明为 应用程序委托。您可以按照以下方式进行操作:

// SampleAppDelegate.h
#import <Foundation/Foundation.h>

@class AsyncSocket;

@interface SampleAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    AsyncSocket *socket;
}

@property (nonatomic,retain) AsyncSocket *socket;

// SampleAppDelegate.m
#import <SampleAppDelegate.h>
#import <AsyncSocket.h>

@implementation SampleAppDelegate

@synthesize socket;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [window addSubview:[splitViewController view]];
    [window makeKeyAndVisible];
    self.socket = [[AsyncSocket alloc] init]; // I can't remember this off the top of my head!
}

从现在起,您可以通过简单地执行以下操作来访问您的套接字:

AsyncSocket *socket = [UIApplication sharedApplication] delegate] socket];

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:

// SampleAppDelegate.h
#import <Foundation/Foundation.h>

@class AsyncSocket;

@interface SampleAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    AsyncSocket *socket;
}

@property (nonatomic,retain) AsyncSocket *socket;

// SampleAppDelegate.m
#import <SampleAppDelegate.h>
#import <AsyncSocket.h>

@implementation SampleAppDelegate

@synthesize socket;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [window addSubview:[splitViewController view]];
    [window makeKeyAndVisible];
    self.socket = [[AsyncSocket alloc] init]; // I can't remember this off the top of my head!
}

From this point on you can access your socket by simply doing:

AsyncSocket *socket = [UIApplication sharedApplication] delegate] socket];
与酒说心事 2024-10-01 00:25:24

您确实应该尽可能避免全局状态(请参阅 Sven 的链接)。一般来说,您可能最好将某种对象传递给其他对象。

例如,我正在开发的当前应用程序有一个 ServerInfo 对象,该对象是我在登录之前创建的,并在创建它时传递给每个新的视图控制器,它包含套接字、加密信息、当前消息 ID、用户名等 但是,

要回答如何将其声明为全局的问题,您可以像在 C: 中那样在

login.h: 中

extern AsyncSocket *g_socket;

在 login.m: 中进行操作,

AsyncSocket *g_socket;

然后在创建套接字时

g_socket = [[AsyncSocket alloc] init];

以及在中使用它时进行操作。其他文件:

#import "login.h"

...

[g_socket sendData:...];

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:

extern AsyncSocket *g_socket;

in login.m:

AsyncSocket *g_socket;

then when you create the socket:

g_socket = [[AsyncSocket alloc] init];

and when you want to use it in other files:

#import "login.h"

...

[g_socket sendData:...];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文