无法响应目标 C 中的警告



我收到警告:

RS232Msg 无法响应 "-initWithRS232MsgRawEncoded">

代码是

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

initWithRS232MsgRawEncodedRS232Msg的派生类。pMsg是指向RS232Msg的指针。创建消息是在RS232Msg中声明的方法如何使其访问?

如果在派生自RS232Msg的类中定义了initWithRS232MsgRawEncoded,则不能将该选择器与RS232Msg*一起使用。

如果我正确理解您要做什么,您希望通过使用原始编码初始化它们来添加另一种创建RS232Msg对象的可能性。

您可以通过不同的方式做到这一点。一种是创建一种"工厂"类(根据GoF模式,它不会是正统的工厂,但这并不重要(。此类可以具有一个完全是您的initWithRS232MsgRawEncoded的静态函数。

您拥有的另一个选项是为RS232定义一个类别,然后将initWithRS232MsgRawEncoded添加到其中。类别是一种扩展类而无需对其进行子类化的方法。这是您在情况下如何做到这一点的骨架:

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

最新更新