xcode iOS/Objective-C 上的 Google 协议缓冲区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8759202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:55:17  来源:igfitidea点击:

Google Protocol Buffers on iOS/Objective-C

objective-ciosxcodeprotocol-buffers

提问by Filipe Mota

I need help to configure/use protobuf in Objective-C for a iOS app. I've tried all but I keep getting errors on xcode. Did anyone manage to make protobuf work well in Objective-C/iOS?

我需要帮助在 Objective-C 中为 iOS 应用程序配置/使用 protobuf。我已经尝试了所有,但我一直在 xcode 上出错。有没有人设法使 protobuf 在 Objective-C/iOS 中运行良好?

采纳答案by Krumelur

I have used it to some extent on iOS, and with metasyntactic's extension, it works really well. I even managed to get the code generation as a custom build step in Xcode. We switched to Thrift for our project (for other reasons), so my apologies if some details below are wrong, but in general this is how to do it.

我在 iOS 上使用过它,并且使用metasyntactic 的 extension,它运行得非常好。我什至设法将代码生成作为 Xcode 中的自定义构建步骤。我们为我们的项目切换到 Thrift(出于其他原因),所以如果下面的某些细节有误,我深表歉意,但总的来说,这是如何做到的。

  1. In Xcode 4.2, open the target properties, go to the "Build Rules" tab and create a new build rule for "*.proto" files
  2. In "Using:", choose "custom script" and use a script like this:

    protoc --plugin=/usr/local/bin/protoc-gen-objc $INPUT_FILE_PATH --objc_out=$DERIVED_FILES_DIR

  3. In Output files, add the generated files (there should be two files, $(INPUT_FILE_BASE).pb.mand $(INPUT_FILE_BASE).pb.h.

  4. Add the .proto files to your project.
  1. 在 Xcode 4.2 中,打开目标属性,转到“构建规则”选项卡并为“*.proto”文件创建一个新的构建规则
  2. 在“使用:”中,选择“自定义脚本”并使用如下脚本:

    protoc --plugin=/usr/local/bin/protoc-gen-objc $INPUT_FILE_PATH --objc_out=$DERIVED_FILES_DIR

  3. 在输出文件中,添加生成的文件(应该有两个文件,$(INPUT_FILE_BASE).pb.m$(INPUT_FILE_BASE).pb.h.

  4. 将 .proto 文件添加到您的项目中。