我创建了一个类型为NSObject的新类,该类创建了两个文件-.h和.m文件。这是两个文件中的代码:
套接字连接
#import <Foundation/Foundation.h>
@interface SocketConnection : NSObject
{
}
+ (SocketConnection *)getInstance;
@end
套接字连接
#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)
最佳答案
您需要#import .m文件顶部的#import“ Socket.h”。
这里的错误
"_OBJC_CLASS_$_Socket", referenced from:
objc-class-ref in SocketConnection.o
表示SocketConnection引用了它不知道的名为“ Socket”的Objective-C类。