2012-06-01 61 views
0

我創建了一個新類NSObject類,它創建了兩個文件 - 一個.h和一個.m文件。下面是來自兩個文件的代碼:Xcode在創建新類時生成錯誤

SocketConnection.h

#import <Foundation/Foundation.h> 

@interface SocketConnection : NSObject 
{ 

} 

+ (SocketConnection *)getInstance; 

@end 

SocketConnection.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) 

回答

3

你需要你的.m文件#IMPORT 「socket.h中」 排在首位。

這裏

"_OBJC_CLASS_$_Socket", referenced from: 
objc-class-ref in SocketConnection.o 

錯誤是說,是SocketConnection處引用名爲「套接字」,它不知道的Objective-C類。