2011-06-08 56 views
-2

我得到一个警告:不能在Objective C到预警响应

RS232Msg无法 回应 “-initWithRS232MsgRawEncoded”

代码为

-(void)createMessage 
{ 
    RS232Msg* pMsg; 
    //pMsg = new RS232MsgRawEncoded(static_cast<int>nMessageNumber); in cpp 
    pMsg = [pMsg initWithRS232MsgRawEncoded:(int)nMessageNumber]; 
} 

initWithRS232MsgRawEncodedRS232Msg的派生类。 和pMsg是指向RS232Msg的指针。 createMessage是一种在RS232Msg中声明的方法如何使其访问?

回答

0

如果从RS232Msg派生您不能使用选择与RS232Msg*类定义initWithRS232MsgRawEncoded

如果我正确理解你正在尝试做什么,你想添加一个更多的可能性,创建RS232Msg对象通过初始化他们与原始编码。

你可以用不同的方式做到这一点。一种是创建一种“工厂”类(根据GoF模式,它不是一个正统工厂,但这并不重要)。这个类可以有一个静态函数,就是你的initWithRS232MsgRawEncoded

您拥有的另一个选项是为RS232定义一个类别,然后将initWithRS232MsgRawEncoded添加到该类别中。类别是扩展类的一种方法,无需对它们进行子类化。这是你如何处理你的情况的骨架:

@interface RS232 (MyRS232Extension) 
    (id)initWithRS232MsgRawEncoded:....; 
@end 

@implementationRS232 (MyRS232Extension) 

    .... 

@end