"重复"类型字段未在Google协议缓冲区目标C中编译

时间:2013-03-05 09:44:05

标签: ios objective-c protocol-buffers

我正在使用an ARC enabled objective c version of protocol buffer,我已将其与XCode 4.5集成。我编译了一个简单的proto文件,对其进行了序列化和反序列化,以检查它是否正常工作。 现在我添加了一个“重复”类型字段,我收到以下错误 -

  

ARC语义问题:“PBAppendableArray”没有可见的@interface声明选择器“objectAtIndex:”

我的原型文件 -

message Person
{
required int32 id=1;
required string name=2;
repeated string email=3;
}

Person.pb.h

 // Generated by the protocol buffer compiler.  DO NOT EDIT!

    #import <ProtocolBuffers/ProtocolBuffers.h>

    @class Person;
    @class Person_Builder;
    #ifndef __has_feature
      #define __has_feature(x) 0 // Compatibility with non-clang compilers.
    #endif // __has_feature

    #ifndef NS_RETURNS_NOT_RETAINED
      #if __has_feature(attribute_ns_returns_not_retained)
        #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
      #else
        #define NS_RETURNS_NOT_RETAINED
      #endif
    #endif


    @interface PersonRoot : NSObject {
    }
    + (PBExtensionRegistry*) extensionRegistry;
    + (void) registerAllExtensions:(PBMutableExtensionRegistry*) registry;
    @end

    @interface Person : PBGeneratedMessage {
    @private
      BOOL hasId_:1;
      BOOL hasName_:1;
      int32_t id;
      NSString* name;
      PBAppendableArray * emailArray;
    }
    - (BOOL) hasId;
    - (BOOL) hasName;
    @property (readonly) int32_t id;
    @property (readonly, strong) NSString* name;
    @property (readonly, strong) PBArray * email;
    - (NSString*)emailAtIndex:(NSUInteger)index;

    + (Person*) defaultInstance;
    - (Person*) defaultInstance;

    - (BOOL) isInitialized;
    - (void) writeToCodedOutputStream:(PBCodedOutputStream*) output;
    - (Person_Builder*) builder;
    + (Person_Builder*) builder;
    + (Person_Builder*) builderWithPrototype:(Person*) prototype;
    - (Person_Builder*) toBuilder;

    + (Person*) parseFromData:(NSData*) data;
    + (Person*) parseFromData:(NSData*) data extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
    + (Person*) parseFromInputStream:(NSInputStream*) input;
    + (Person*) parseFromInputStream:(NSInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
    + (Person*) parseFromCodedInputStream:(PBCodedInputStream*) input;
    + (Person*) parseFromCodedInputStream:(PBCodedInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry;
    @end

    @interface Person_Builder : PBGeneratedMessage_Builder {
    @private
      Person* result;
    }

    - (Person*) defaultInstance;

    - (Person_Builder*) clear;
    - (Person_Builder*) clone;

    - (Person*) build;
    - (Person*) buildPartial;

    - (Person_Builder*) mergeFrom:(Person*) other;
    - (Person_Builder*) mergeFromCodedInputStream:(PBCodedInputStream*) input;
    - (Person_Builder*) mergeFromCodedInputStream:(PBCodedInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry;

    - (BOOL) hasId;
    - (int32_t) id;
    - (Person_Builder*) setId:(int32_t) value;
    - (Person_Builder*) clearId;

    - (BOOL) hasName;
    - (NSString*) name;
    - (Person_Builder*) setName:(NSString*) value;
    - (Person_Builder*) clearName;

    - (PBAppendableArray *)email;
    - (NSString*)emailAtIndex:(NSUInteger)index;
    - (Person_Builder *)addEmail:(NSString*)value;
    - (Person_Builder *)setEmailArray:(NSArray *)array;
    - (Person_Builder *)setEmailValues:(const NSString* *)values count:(NSUInteger)count;
    - (Person_Builder *)clearEmail;
    @end

Person.pb.m

// Generated by the protocol buffer compiler.  DO NOT EDIT!

#import "Person.pb.h"

@implementation PersonRoot
static PBExtensionRegistry* extensionRegistry = nil;
+ (PBExtensionRegistry*) extensionRegistry {
  return extensionRegistry;
}

+ (void) initialize {
  if (self == [PersonRoot class]) {
    PBMutableExtensionRegistry* registry = [PBMutableExtensionRegistry registry];
    [self registerAllExtensions:registry];
    extensionRegistry = registry;
  }
}
+ (void) registerAllExtensions:(PBMutableExtensionRegistry*) registry {
}
@end

@interface Person ()
@property int32_t id;
@property (strong) NSString* name;
@property (strong) PBAppendableArray * emailArray;
@end

@implementation Person

- (BOOL) hasId {
  return !!hasId_;
}
- (void) setHasId:(BOOL) value {
  hasId_ = !!value;
}
@synthesize id;
- (BOOL) hasName {
  return !!hasName_;
}
- (void) setHasName:(BOOL) value {
  hasName_ = !!value;
}
@synthesize name;
@synthesize emailArray;
@dynamic email;
- (id) init {
  if ((self = [super init])) {
    self.id = 0;
    self.name = @"";
  }
  return self;
}
static Person* defaultPersonInstance = nil;
+ (void) initialize {
  if (self == [Person class]) {
    defaultPersonInstance = [[Person alloc] init];
  }
}
+ (Person*) defaultInstance {
  return defaultPersonInstance;
}
- (Person*) defaultInstance {
  return defaultPersonInstance;
}
- (NSArray *)email {
  return emailArray;
}
- (NSString*)emailAtIndex:(NSUInteger)index {
  return [emailArray objectAtIndex:index];
}
- (BOOL) isInitialized {
  if (!self.hasId) {
    return NO;
  }
  if (!self.hasName) {
    return NO;
  }
  return YES;
}
- (void) writeToCodedOutputStream:(PBCodedOutputStream*) output {
  if (self.hasId) {
    [output writeInt32:1 value:self.id];
  }
  if (self.hasName) {
    [output writeString:2 value:self.name];
  }
  const NSUInteger emailArrayCount = self.emailArray.count;
  if (emailArrayCount > 0) {
    const NSString* *values = (const NSString* *)self.emailArray.data;
    for (NSUInteger i = 0; i < emailArrayCount; ++i) {
      [output writeString:3 value:values[i]];
    }
  }
  [self.unknownFields writeToCodedOutputStream:output];
}
- (int32_t) serializedSize {
  int32_t size = memoizedSerializedSize;
  if (size != -1) {
    return size;
  }

  size = 0;
  if (self.hasId) {
    size += computeInt32Size(1, self.id);
  }
  if (self.hasName) {
    size += computeStringSize(2, self.name);
  }
  {
    int32_t dataSize = 0;
    const NSUInteger count = self.emailArray.count;
    const NSString* *values = (const NSString* *)self.emailArray.data;
    for (NSUInteger i = 0; i < count; ++i) {
      dataSize += computeStringSizeNoTag(values[i]);
    }
    size += dataSize;
    size += 1 * count;
  }
  size += self.unknownFields.serializedSize;
  memoizedSerializedSize = size;
  return size;
}
+ (Person*) parseFromData:(NSData*) data {
  return (Person*)[[[Person builder] mergeFromData:data] build];
}
+ (Person*) parseFromData:(NSData*) data extensionRegistry:(PBExtensionRegistry*) extensionRegistry {
  return (Person*)[[[Person builder] mergeFromData:data extensionRegistry:extensionRegistry] build];
}
+ (Person*) parseFromInputStream:(NSInputStream*) input {
  return (Person*)[[[Person builder] mergeFromInputStream:input] build];
}
+ (Person*) parseFromInputStream:(NSInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry {
  return (Person*)[[[Person builder] mergeFromInputStream:input extensionRegistry:extensionRegistry] build];
}
+ (Person*) parseFromCodedInputStream:(PBCodedInputStream*) input {
  return (Person*)[[[Person builder] mergeFromCodedInputStream:input] build];
}
+ (Person*) parseFromCodedInputStream:(PBCodedInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry {
  return (Person*)[[[Person builder] mergeFromCodedInputStream:input extensionRegistry:extensionRegistry] build];
}
+ (Person_Builder*) builder {
  return [[Person_Builder alloc] init];
}
+ (Person_Builder*) builderWithPrototype:(Person*) prototype {
  return [[Person builder] mergeFrom:prototype];
}
- (Person_Builder*) builder {
  return [Person builder];
}
- (Person_Builder*) toBuilder {
  return [Person builderWithPrototype:self];
}
- (void) writeDescriptionTo:(NSMutableString*) output withIndent:(NSString*) indent {
  NSUInteger listCount = 0;
  if (self.hasId) {
    [output appendFormat:@"%@%@: %@\n", indent, @"id", [NSNumber numberWithInt:self.id]];
  }
  if (self.hasName) {
    [output appendFormat:@"%@%@: %@\n", indent, @"name", self.name];
  }
  for (NSString* element in self.emailArray) {
    [output appendFormat:@"%@%@: %@\n", indent, @"email", element];
  }
  [self.unknownFields writeDescriptionTo:output withIndent:indent];
}
- (BOOL) isEqual:(id)other {
  if (other == self) {
    return YES;
  }
  if (![other isKindOfClass:[Person class]]) {
    return NO;
  }
  Person *otherMessage = other;
  return
      self.hasId == otherMessage.hasId &&
      (!self.hasId || self.id == otherMessage.id) &&
      self.hasName == otherMessage.hasName &&
      (!self.hasName || [self.name isEqual:otherMessage.name]) &&
      [self.emailArray isEqualToArray:otherMessage.emailArray] &&
      (self.unknownFields == otherMessage.unknownFields || (self.unknownFields != nil && [self.unknownFields isEqual:otherMessage.unknownFields]));
}
- (NSUInteger) hash {
  NSUInteger hashCode = 7;
  NSUInteger listCount = 0;
  if (self.hasId) {
    hashCode = hashCode * 31 + [[NSNumber numberWithInt:self.id] hash];
  }
  if (self.hasName) {
    hashCode = hashCode * 31 + [self.name hash];
  }
  for (NSString* element in self.emailArray) {
    hashCode = hashCode * 31 + [element hash];
  }
  hashCode = hashCode * 31 + [self.unknownFields hash];
  return hashCode;
}
@end

@interface Person_Builder()
@property (strong) Person* result;
@end

@implementation Person_Builder
@synthesize result;
- (id) init {
  if ((self = [super init])) {
    self.result = [[Person alloc] init];
  }
  return self;
}
- (PBGeneratedMessage*) internalGetResult {
  return result;
}
- (Person_Builder*) clear {
  self.result = [[Person alloc] init];
  return self;
}
- (Person_Builder*) clone {
  return [Person builderWithPrototype:result];
}
- (Person*) defaultInstance {
  return [Person defaultInstance];
}
- (Person*) build {
  [self checkInitialized];
  return [self buildPartial];
}
- (Person*) buildPartial {
  Person* returnMe = result;
  self.result = nil;
  return returnMe;
}
- (Person_Builder*) mergeFrom:(Person*) other {
  if (other == [Person defaultInstance]) {
    return self;
  }
  if (other.hasId) {
    [self setId:other.id];
  }
  if (other.hasName) {
    [self setName:other.name];
  }
  if (other.emailArray.count > 0) {
    if (result.emailArray == nil) {
      result.emailArray = [[NSMutableArray alloc] initWithArray:other.emailArray];
    } else {
      [result.emailArray addObjectsFromArray:other.emailArray];
    }
  }
  [self mergeUnknownFields:other.unknownFields];
  return self;
}
- (Person_Builder*) mergeFromCodedInputStream:(PBCodedInputStream*) input {
  return [self mergeFromCodedInputStream:input extensionRegistry:[PBExtensionRegistry emptyRegistry]];
}
- (Person_Builder*) mergeFromCodedInputStream:(PBCodedInputStream*) input extensionRegistry:(PBExtensionRegistry*) extensionRegistry {
  PBUnknownFieldSet_Builder* unknownFields = [PBUnknownFieldSet builderWithUnknownFields:self.unknownFields];
  while (YES) {
    int32_t tag = [input readTag];
    switch (tag) {
      case 0:
        [self setUnknownFields:[unknownFields build]];
        return self;
      default: {
        if (![self parseUnknownField:input unknownFields:unknownFields extensionRegistry:extensionRegistry tag:tag]) {
          [self setUnknownFields:[unknownFields build]];
          return self;
        }
        break;
      }
      case 8: {
        [self setId:[input readInt32]];
        break;
      }
      case 18: {
        [self setName:[input readString]];
        break;
      }
      case 26: {
        [self addEmail:[input readString]];
        break;
      }
    }
  }
}
- (BOOL) hasId {
  return result.hasId;
}
- (int32_t) id {
  return result.id;
}
- (Person_Builder*) setId:(int32_t) value {
  result.hasId = YES;
  result.id = value;
  return self;
}
- (Person_Builder*) clearId {
  result.hasId = NO;
  result.id = 0;
  return self;
}
- (BOOL) hasName {
  return result.hasName;
}
- (NSString*) name {
  return result.name;
}
- (Person_Builder*) setName:(NSString*) value {
  result.hasName = YES;
  result.name = value;
  return self;
}
- (Person_Builder*) clearName {
  result.hasName = NO;
  result.name = @"";
  return self;
}
- (NSMutableArray *)email {
  return result.emailArray;
}
- (NSString*)emailAtIndex:(NSUInteger)index {
  return [result emailAtIndex:index];
}
- (Person_Builder *)addEmail:(NSString*)value {
  if (result.emailArray == nil) {
    result.emailArray = [[NSMutableArray alloc]init];
  }
  [result.emailArray addObject:value];
  return self;
}
- (Person_Builder *)setEmailArray:(NSArray *)array {
  result.emailArray = [[NSMutableArray alloc] initWithArray:array];
  return self;
}
- (Person_Builder *)clearEmail {
  result.emailArray = nil;
  return self;
}
@end

1 个答案:

答案 0 :(得分:3)

为可能稍后检查此问题的人留下这个答案。

最好直接在Xcode中使用C ++ protobuf文件。在提到的protobuf的ios版本中,必需的字段根本不会编译。 Check this answer在项目中添加protobuf。