diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index a2acb4b7086..8c8e30986fa 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -94,7 +94,8 @@ switch ([key intValue]) { case GRPC_OP_SEND_INITIAL_METADATA: // TODO(jcanizales): Name the type of current->data.send_initial_metadata in the C library so a pointer to it can be returned from methods. - current->data.send_initial_metadata.count = [operations[key] grpc_toMetadataArray:&send_metadata]; + current->data.send_initial_metadata.count = [operations[key] count]; + [operations[key] grpc_getMetadataArray:&send_metadata]; current->data.send_initial_metadata.metadata = send_metadata; opBlock = ^{ gpr_free(send_metadata); diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h index f6aeed35c0c..fec2adb2120 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h @@ -36,5 +36,5 @@ @interface NSDictionary (GRPC) + (instancetype)grpc_dictionaryFromMetadata:(struct grpc_metadata *)entries count:(size_t)count; -- (size_t)grpc_toMetadataArray:(grpc_metadata **)metadata; +- (void)grpc_getMetadataArray:(grpc_metadata **)metadata; @end diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m index 1df06c167fa..1b8e6a3a173 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m @@ -55,23 +55,22 @@ return metadata; } -- (size_t)grpc_toMetadataArray:(grpc_metadata **)metadata { - size_t count = 0; - size_t capacity = 0; +- (void)grpc_getMetadataArray:(grpc_metadata **)metadata { + *metadata = gpr_malloc([self count] * sizeof(grpc_metadata)); + int i = 0; for (id key in self) { - capacity += [self[key] count]; - } - *metadata = gpr_malloc(capacity * sizeof(grpc_metadata)); - for (id key in self) { - id value_array = self[key]; - for (id value in value_array) { - grpc_metadata *current = &(*metadata)[count]; - current->key = [key UTF8String]; + id value = self[key]; + grpc_metadata *current = &(*metadata)[i]; + current->key = [key UTF8String]; + if ([value isKindOfClass:[NSData class]]) { + current->value = [value bytes]; + } else if ([value isKindOfClass:[NSString class]]) { current->value = [value UTF8String]; - current->value_length = [value length]; - count += 1; + } else { + [NSException raise:NSInvalidArgumentException format:@"Metadata values must be NSString or NSData."]; } + current->value = [value UTF8String]; + i += 1; } - return count; } @end