我是Objective C的新手。我正在尝试一个简单的程序。没有异常。无法
修复它。
#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
#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
#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
#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
#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;
}
输出
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
答案 0 :(得分:2)
您无法像这样访问实例变量。你需要一个getter方法或一个属性。
在您的界面中:
@property( readonly ) unsigned int m_nTxImageSize;
并在实施中
@synthesize m_nTxImageSize
然后你可以访问它:
pack.m_abTxMsgImage
修改强>
对于您的数组属性,您应该能够使用指针属性:
@property( readonly ) char * m_abTxMsgImage;