2011-05-06 56 views
0

我有m_pData=1099656static聲明在classstatic behaviour-> objective C

mainNSLog(@"Testing:%d",m_pData);

其打印我0

如何在主函數中的類中獲得相同的值m_pData

我無法使用obj.m_pDataobj->m_pData來訪問變量。

編輯:

test2.m 
---------- 


#import <Foundation/Foundation.h> 
#import "data_derived.h" 

int main (int argc, const char * argv[]) { 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    data* dat = [data alloc]; 
    requestSession* session = [requestSession alloc]; 
    [session init]; 

    [dat TxCreateImage:RM_REQUEST_SESSION]; 
    NSLog(@"Testing:%d",m_pData); //the static variable is not printing the value its holding.Its printing Zero.If printed the same variable inside the class it gives some numbers. 
    [dat dataBuffer:&m_pData withLen:&m_uDataSize]; //here the actual values of static values are not passed.when printed both of them contains zero values. 

    [pool drain]; 
    return 0; 

} 

data.h 
-------- 

#import <Foundation/Foundation.h> 
#import "remote.h" 

static int m_nMessageId;  //Message ID 
static int m_uSessionId;  //Session ID 
static int m_chSequenceChar; //Sequence ID 

static int* m_pData;   //Integer buffer to carry data 
static int m_uDataSize;  //Datasize 

@interface data : NSObject { 

    @public 


} 

- (id)initWithID:(int) uMessageId withData:(id)pData withSize:(size_t) uDataSize; 
+ (void)initialize; 
- (void)dealloc; 
- (id) dataBuffer:(int**)m_Data withLen:(int**)uLen; 
- (BOOL) TxCreateImage:(int)messageId; 

@end 

data.m 
--------- 

#import "data.h" 

#define ENCODED_MSG_DATA_OFFSET 8 

@implementation data 

+ (void)initialize 
{ 
    m_uSessionId = 0; 
    m_chSequenceChar= 0; 

    // Initialize values from derived class 
    m_nMessageId = 0; 
    m_pData   = 0;    
    m_uDataSize  = 0; 

} 

- (id) initWithID:(int) uMessageId withData:(id)pData withSize:(size_t) uDataSize 
{ 
     if(self=[super init]) 
     { 
      // Initialize the member variables 
      m_uSessionId = 0xFF; 
      m_chSequenceChar= 10; 

      // Initialize values from derived class 
      m_nMessageId = uMessageId; 
      m_pData   = (int*)pData;    
      m_uDataSize  = (int)uDataSize;    
     } 
     NSLog(@"Data size:%d",uDataSize); 
     NSLog(@"m_pData:%d",m_pData); 
     NSLog(@"pData:%d",pData); 

     return self;  
} 

- (id) dataBuffer:(int**)m_Data withLen:(int**)uLen 
{ 
     if (m_uDataSize <= RMH_MAX_ENCODED_LENGTH) 
     { 
     int abBigEndian[RMH_MESSAGE_MAX_SIZE]; 
     memcpy(abBigEndian,m_Data,m_uDataSize); 
     NSLog(@"m_Data:%d",*m_Data); 
     NSLog(@"abBigEndian:%d",abBigEndian); 
     uLen += ENCODED_CRC_BYTE_LEN + 1; 
     NSLog(@"%d",*uLen); 
     } 

     NSLog(@"END!"); 
     return self;  
} 

- (BOOL) TxCreateImage:(int)messageId 
{ 
    char pData[4096]; 
    sprintf(pData,"%x %d %d %d %x",ASCII_STX,m_uSessionId,m_chSequenceChar,m_nMessageId,ASCII_ETX); //uLen = ENCODED_MSG_DATA_OFFSET; 
    NSLog(@"%s",pData); 
    return YES; 
} 


- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

data_derived.h 
--------------------- 


#import <Foundation/Foundation.h> 
#import "data.h" 

#define DECLARE_RS232_NEWMSG(ClassID)\ 
enum         \ 
{         \ 
    ID = ClassID      \ 
};         \ 

@interface requestSession : data { 

@public 
    DECLARE_RS232_NEWMSG(RM_REQUEST_SESSION); 
    struct RMH_REQUEST_SESSION_MSG st; 
} 

-(id)init; 
-(void)dealloc; 

@end 

data_derived.m 
--------------------- 


#import "data_derived.h" 

@implementation requestSession 

- (id)init 
{ 
    size_t asize = sizeof(st); 
    st.uDeviceID = RS232_PROTOCOL_DEVICE_ID; 
    st.uProtocolVersion = RS232_VERSION; 
    memset(st.uReserved,0x00,sizeof(st.uReserved)); 
    NSLog(@"Address of the structure:%d",&st); 
    self=[super initWithID:ID withData:(id)&st withSize:asize]; 
    if (self) { 

    } 
    return self; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 
+1

向我們展示代碼 – 2011-05-06 10:02:00

+0

@Frank C:我編輯了代碼。 – Angus 2011-05-06 10:14:24

回答

2

不要你的靜態成員放在.h文件中。將它們移到data.m.

另外關於static關鍵字 - 在C(和Objective C中,C是明確的超集)中,將static與全局變量一起使用時(與您一樣),它僅表示這些變量對於您聲明的文件是本地的這些變量。因此,全局變量無論如何都只有一個實例,有static修飾符或不帶。如果你不使用static,那麼你就可以訪問其他文件,這些變量與external聲明,如:

// file1.m 
int variable = 4; 

// file2.m 
external int variable; // variable == 4 

這就是爲什麼你的代碼是你打印0 m_pData在test2.m是不一樣的m_pData你有data.m.如果沒有static修飾符,您將得到一個鏈接器錯誤。

你可能想爲靜態成員編寫getters/setters。喜歡:

+ (int *)pData { 
    return m_pData; // or smth like memcpy 
}