开发者

static behaviour->objective C

开发者 https://www.devze.com 2023-03-04 14:36 出处:网络
I have this static declaration of m_pData=1099656 in a class. In main NSLog(@\"Testing:%d\",m_pData); Its printing me 0.

I have this static declaration of m_pData=1099656 in a class.

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

Its printing me 0.

How to get the same value of m_pData in the class in the main function too.

I cant use obj.m_pData or obj->m_pData to acces the variable.

EDITED:

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 dea开发者_如何学Clloc];
}

@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


Don't place your static members in .h file. Move them to data.m.

Also about static keyword - in C (and Objective C, which is clear superset to C), when using static with global variables (like you do), it only indicates that this variables will be local to the file you declared these variable in. So global variables anyway have only one instance, with static modifier or without. If you don't use static, then you can access these variables from other files with external declaration, like:

// file1.m
int variable = 4;

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

That's why your code is printing you 0. m_pData in test2.m is not the same m_pData you have in data.m. Without static modifier you would get a linker error.

And you might want to write getters/setters for static members. Like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号