0

I am new to Objective C.I am trying out a simple program.Got an exception.Could not able

to fix it up.

packet.h

#import <Foundation/Foundation.h>

#define RS232_PROTOCOL_DEVICE_ID 0xD8
#define RS232_MAJOR_REV (1)
#define RS232_MINOR_REV (23)
#define RS232_VERSION (RS232_MAJOR_REV * 10000 + RS232_MINOR_REV)

struct msg {

    int deviceId;
    int version;
    int reserved[5];

}a;

enum  {

    RMH_MESSAGE_MAX_SIZE =4096

};

@interface Packet : NSObject {
    unsigned char m_abTxMsgImage[RMH_MESSAGE_MAX_SIZE];
    unsigned int m_nTxImageSize;
};

+ (void)initialize;
- (void)send;

@end

packet.m

#import "Packet.h"


@implementation Packet

- (id)init
{
    if (self=[super init]) {
     m_nTxImageSize=0;
    }
    return self;
}
+ (void)initialize
{
    a.deviceId =RS232_PROTOCOL_DEVICE_ID;
    a.version = RS232_VERSION;
    a.reserved[0]=0;
    a.reserved[1]=0;
    a.reserved[2]=0;
    a.reserved[3]=0;
    a.reserved[4]=0;
}

- (void)send
{
    NSLog(@"DeviceId:%d   VersionNo:%d",a.deviceId,a.version);
}

@end

data.h

#import <Foundation/Foundation.h>

#define ENCODED_MSG_DATA_OFFSET 8 
#define ENCODED_CRC_BYTE_LEN 4  

#define ASCII_STX 0x02 

typedef enum
{
    eRs232MsgStatus_Valid

} eRs232MsgStatus;

@interface data : NSObject {

    //char tmp[100];
    //char tmp1[101];
    int m_uSessionId;
    int m_chSequenceChar;
    int m_nMessageId;
    int m_uVersion;

}

- (BOOL) TxCreateImage:(int*)pData :(int)uLen;

@end

data.m

#import "data.h"


@implementation data

- (BOOL)TxCreateImage:(int*)pData :(int)uLen
{
    NSLog(@"Hello!");
    sprintf((char *)pData,"%c%02X%c%02X%02X",ASCII_STX,m_uSessionId,m_chSequenceChar,m_nMessageId,m_uVersion);
    uLen = ENCODED_MSG_DATA_OFFSET;
    uLen += ENCODED_CRC_BYTE_LEN + 1;
    return YES;
}

@end

main.m

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Packet* pack = [[Packet alloc]init];
    [pack send];

    data* dat = [[data alloc]init];
    [dat TxCreateImage:(int*)[pack m_abTxMsgImage] :[pack m_nTxImageSize]];

    [pool drain];
    return 0;
}

Output

2011-05-04 17:34:00.006 Test-packetCreation[4982:a0f] DeviceId:216   VersionNo:10023
2011-05-04 17:34:00.009 Test-packetCreation[4982:a0f] -[Packet m_nTxImageSize]: unrecognized selector sent to instance 0x100831e00
2011-05-04 17:34:00.009 Test-packetCreation[4982:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Packet m_nTxImageSize]: unrecognized selector sent to instance 0x100831e00'
*** Call stack at first throw:
(
    0   CoreFoundation    0x00007fff84aaccc4 __exceptionPreprocess + 180
    1   libobjc.A.dylib   0x00007fff8167b0f3 objc_exception_throw + 45
    2   CoreFoundation    0x00007fff84b06140 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0
    3   CoreFoundation    0x00007fff84a7ecdf ___forwarding___ + 751
    4   CoreFoundation    0x00007fff84a7ae28 _CF_forwarding_prep_0 + 232
    5   Test-packetCreation    0x0000000100001aed main + 209
    6   Test-packetCreation    0x0000000100001a14 start + 52
    7   ???                    0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Program received signal:  gSIGABRTh.
sharedlibrary apply-load-rules all
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
Angus
  • 12,133
  • 29
  • 96
  • 151
  • possible duplicate of [Getting Exceptions in function call-Objective C](http://stackoverflow.com/questions/5882745/getting-exceptions-in-function-call-objective-c) – DarkDust May 04 '11 at 12:05
  • You have asked exactly the same question a few minutes ago. Please edit your original question instead of posting a new one. – DarkDust May 04 '11 at 12:05
  • @DarkDust:Sorry the question i asked was only a small part of it and was not the entire code.I thought it would be difficult to debug the error without the entire code and have posted as a new one here. – Angus May 04 '11 at 12:16

1 Answers1

2

You can't access instance variables like that. You need either a getter method or a property.

In your interface:

@property( readonly ) unsigned int m_nTxImageSize;

And in the implementation

@synthesize m_nTxImageSize

Then you can access it:

pack.m_abTxMsgImage

EDIT

For your array property, you should be able to use a pointer property:

@property( readonly ) char * m_abTxMsgImage;
Macmade
  • 52,708
  • 13
  • 106
  • 123
  • Thanks.I could able to clear up the error.Did as you told.But how to access the m_abTxMsgImage. I declared as this @property(readonly)unsigned char m_abTxMsgImage[RMH_MESSAGE_MAX_SIZE]; I am getting errors. @synthesize m_abTxMsgImage[RMH_MESSAGE_MAX_SIZE]; – Angus May 04 '11 at 12:27
  • Use only the name in the @synthesize instruction: @synthesize m_abTxMsgImage – Macmade May 04 '11 at 13:18
  • error: type of property 'm_abTxMsgImage' does not match type of ivar 'm_abTxMsgImage'.[I have declared like this in my code @property(readwrite)unsigned char m_abTxMsgImage; unsigned char m_abTxMsgImage[RMH_MESSAGE_MAX_SIZE]; @synthesize m_abTxMsgImage;] – Angus May 04 '11 at 13:31