我创建了一个类型为 NSObject 的新类,它创建了两个文件——一个 .h 和一个 .m 文件。这是两个文件中的代码:
套接字连接.h
#import <Foundation/Foundation.h>
@interface SocketConnection : NSObject
{
}
+ (SocketConnection *)getInstance;
@end
套接字连接.m
#import "SocketConnection.h"
#import "imports.h"
static SocketConnection *sharedInstance = nil;
@implementation SocketConnection
- (id)init
{
self = [super init];
if (self)
{
while(1)
{
Socket *socket;
int port = 11005;
NSString *host = @"199.5.83.63";
socket = [Socket socket];
@try
{
NSMutableData *data;
[socket connectToHostName:host port:port];
[socket readData:data];
// [socket writeString:@"Hello World!"];
// Connection was successful //
[socket retain]; // Must retain if want to use out of this action block.
}
@catch (NSException* exception)
{
NSString *errMsg = [NSString stringWithFormat:@"%@",[exception reason]];
NSLog(errMsg);
socket = nil;
}
}
}
return self;
}
+ (SocketConnection *)getInstance
{
@synchronized(self)
{
if (sharedInstance == nil)
{
sharedInstance = [[SocketConnection alloc] init];
}
}
return sharedInstance;
}
@end
而且我似乎遇到了链接器错误。当我注释掉 SocketConnection.h/SocketConnection.m 中的所有代码时,错误就消失了。我的项目中有几个视图。我有一个名为“imports.h”的头文件,我已经导入了 SocketConnection.h,并在我的 SocketConnection.m 文件中包含了“imports.h”。任何帮助将不胜感激,因为我似乎被困在这里:/。谢谢!
错误:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_Socket", referenced from:
objc-class-ref in SocketConnection.o
(maybe you meant: _OBJC_CLASS_$_SocketConnection)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)