静态行为 - >目标C.

时间:2011-05-06 09:59:37

标签: objective-c

我在static中有m_pData=1099656 class声明。

main NSLog(@"Testing:%d",m_pData);

打印我0

如何在main函数的类中获得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 个答案:

答案 0 :(得分: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 声明从其他文件访问这些变量,例如:

m_pData

这就是你的代码打印你的原因。{test}中的m_pData与data.m中的static不同。如果没有+ (int *)pData { return m_pData; // or smth like memcpy } 修饰符,您将收到链接器错误。

您可能想为静态成员编写getter / setter。像:

{{1}}