如何为参数指定值。
Rs232MsgRawEncoded(const int nMsgId,const INT8U* pSrc=NULL,unsigned int nSize=0);
我在Objective C中声明了上面的cpp方法声明,如下所示
-(id)initWithRS232MsgRawEncoded:(const int)nMsgId withpSrc:(const uint8_t*)pSrc withSize:(unsigned int)nSize;
在函数内部,我正在检查它是否为null或有一些值。
我无法在Objective C中将变量声明为pSrc = NULL和nSize = 0 有没有办法做到这一点?
答案 0 :(得分:0)
Objective-c(以及c)不支持函数的默认参数。
因此,您既可以使用您拥有的功能,也可以每次都将所有参数传递给它。如果不这样做 - 创建省略了一些参数的新函数,并使用默认参数调用'main'函数,例如:
-(id)initWithRS232MsgRawEncoded:(const int)nMsgId{
return [self initWithRS232MsgRawEncoded:nMsgId withpSrc:NULL withSize:0];
}