timesplices in Objective C
I have converted the following code from cpp to Objective C
xyz.h
#define DECLARE_RS232_NEWMSG(ClassName,ClassID) \
+(ClassName*)FromMsg:(RS232Msg*)pMsg \
{ 开发者_如何学C \
ClassName* ivar = [[ClassName alloc]init]; \
return [ pMsg]; \
} \
+(RS232Msg*)NewMsg \
{ \
return new ClassName; \
} \
enum {ID = ClassID};
xyz.m
@implementation RS232MsgGetEventDescriptions
+ (id)getID
{
DECLARE_RS232_NEWMSG(RS232MsgGetEventDescriptions,RM_GET_EVENT_DESCRIPTIONS);
return ID;
}
@end
getting errors when using NewMsg , ID , FromMsg from other files.I have included all the header files.
EDITED:
RS232MsgGetEventDescriptions.h
#define DECLARE_RS232_NEWMSG(ClassName, ClassID) \
enum {ID = ClassID}; \
@interface ClassName: NSObject {} \
+(ClassName*)FromMsg:(RS232Msg*)pMsg; \
+(RS232Msg*)NewMsg; \
@end;
#define DEFINE_RS232_NEWMSG_IMPLEMENTATION(ClassName, ClassID) \
+(ClassName*)FromMsg:(RS232Msg*)pMsg \
{ \
ClassName* ivar = [[ClassName alloc]init]; \
return [ivar pMsg]; \
} \
+(RS232Msg*)NewMsg \
{ \
return [[ClassName alloc]init]; \
} \
+ (id)getID \
{ \
return ID; \
} \
@interface RS232MsgGetEventDescriptions : RS232Msg{
@public
DECLARE_RS232_NEWMSG(RS232MsgGetEventDescriptions,RM_GET_EVENT_DESCRIPTIONS);
}
@end
RS232MsgGetEventDescriptions.m
@implementation RS232MsgGetEventDescriptions
-(id)init
{
if (self = [super init]) {
DEFINE_RS232_NEWMSG_IMPLEMENTATION(RS232MsgGetEventDescriptions,RM_GET_EVENT_DESCRIPTIONS);
}
return self;
}
@end
FromMsg undeclared and NewMsg undeclared error occurs.
error: expected specifier-qualifier-list before 'interface'
error: expected specifier-qualifier-list before '+' token
error: expected specifier-qualifier-list before 'end'
error: expected specifier-qualifier-list before 'interface'
error: expected specifier-qualifier-list before '+' token
error: expected specifier-qualifier-list before 'end'
error: 'NewMsg' undeclared (first use in this function)
You seem to be attempting to declare FromMsg and NewMsg as class methods within the class method getID. Objective-C is like C in this respect: you can't declare functions (or methods) within functions (or methods). Furthermore, if you don't declare ID until you're in the source file then it won't be visible to anyone else as the C conventions are followed in terms of compiling each file separately and in isolation, then linking them all together at the end.
It seems likely you're coming from a Java background, so these practices will probably feel a little primitive. Sadly Objective-C deviates quite a lot from the don't-repeat-yourself principle.
If the point of the #define is to create something like a template, I'd suggest you probably want either two levels of header file (one to define the template, one to use it with an appropriate class name and numeric ID) or to use a more conventional class/subclass relationship.
E.g.
xyzTemplate.h
#define DECLARE_RS232_NEWMSG(ClassName, ClassID) \
enum {ID = ClassID}; \
\
@interface ClassName: NSObject {} \
+(ClassName*)FromMsg:(RS232Msg*)pMsg; \
+(RS232Msg*)NewMsg; \
@end;
#define DEFINE_RS232_NEWMSG_IMPLEMENTATION(ClassName, ClassID) \
@implementation ClassName \
\
+(ClassName*)FromMsg:(RS232Msg*)pMsg \
{ \
ClassName* ivar = [[ClassName alloc] init]; \
return [ivar pMsg]; \
} \
\
+ (RS232Msg*)NewMsg \
{ \
return [[ClassName alloc] init]; \
} \
\
+ (id)getID \
{ \
return ID; \
} \
\
@end
xyz.h
DECLARE_RS232_NEWMSG(RS232MsgGetEventDescriptions,RM_GET_EVENT_DESCRIPTIONS);
xyz.m
DEFINE_RS232_NEWMSG_IMPLEMENTATION(RS232MsgGetEventDescriptions,RM_GET_EVENT_DESCRIPTIONS);
Though the FromMsg declaration looks a bit dodgy (ivar will leak and the pMsg argument isn't used; you'll invoke a method called pMsg regardless of the value of the argument with the same name), and you probably don't want to use the 'id' type in getID since 'id' is an untyped object in Objective-C, so a pointer in implementation terms. You probably want to return an integer type.
精彩评论