0
message HealthOccurrenceCount
{
    required int64 HealthID=1; 
    required int32 OccCount=2; 
    optional bytes wci=3;
}

我想添加基于的数据HealthID;如果HealthID已输入,则程序不应添加新条目,而应仅增加现有条目的OccCount.

HealthOccurrenceCount objHelthOccCount;
if(objHelthOccCount.healthid() == healthID) // Is this right or do I need to iterate all the nodes?
{
    occCount++;  
    objHelthOccCount.set_occcount(occCount);  
}
else       
    occCount = 1;   

这段代码是正确的还是我应该转换HealthID成字符串?

生成的代码:

// required int64 HealthID = 1;
inline bool has_healthid() const;
inline void clear_healthid();
static const int kHealthIDFieldNumber = 1;
inline ::google::protobuf::int64 healthid() const;
inline void set_healthid(::google::protobuf::int64 value);
4

1 回答 1

0

根据文档,每个单数(必需或可选)字段都有一个 has_ 方法,如果该字段已设置,则返回 true。

您的代码将类似于:

        HealthOccurrenceCount objHelthOccCount;
        if(objHelthOccCount.has_healthid())
        {
            occCount++;  
            objHelthOccCount.set_occcount(occCount);  
        }
        else       
            occCount = 1; 
于 2015-08-24T14:01:59.203 回答