Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 1 | Protocol Buffers - Google's data interchange format |
| 2 | =================================================== |
| 3 | |
Feng Xiao | 2dcd6ae | 2018-07-22 17:14:24 -0700 | [diff] [blame] | 4 | [](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_cocoapods_integration%2Fcontinuous) [](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_ios_debug%2Fcontinuous) [](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_ios_release%2Fcontinuous) [](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fobjectivec_osx%2Fcontinuous) |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 5 | |
| 6 | Copyright 2008 Google Inc. |
| 7 | |
| 8 | This directory contains the Objective C Protocol Buffers runtime library. |
| 9 | |
| 10 | Requirements |
| 11 | ------------ |
| 12 | |
Dongjoon Hyun | 7b08d49 | 2016-01-11 14:52:01 -0800 | [diff] [blame] | 13 | The Objective C implementation requires: |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 14 | |
| 15 | - Objective C 2.0 Runtime (32bit & 64bit iOS, 64bit OS X). |
dmaclach | 6f9d488 | 2020-01-23 13:38:16 -0800 | [diff] [blame] | 16 | - Xcode 10.3 (or later). |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 17 | - The library code does *not* use ARC (for performance reasons), but it all can |
| 18 | be called from ARC code. |
| 19 | |
| 20 | Installation |
| 21 | ------------ |
| 22 | |
leovitch | 2804902 | 2018-05-29 21:08:00 +0900 | [diff] [blame] | 23 | The distribution pulled from github includes the sources for both the |
| 24 | compiler (protoc) and the runtime (this directory). After cloning the distribution |
| 25 | and needed submodules ([see the src directory's README](../src/README.md)), |
| 26 | to build the compiler and run the runtime tests, you can use: |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 27 | |
| 28 | $ objectivec/DevTools/full_mac_build.sh |
| 29 | |
| 30 | This will generate the `src/protoc` binary. |
| 31 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 32 | Building |
| 33 | -------- |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 34 | |
| 35 | There are two ways to include the Runtime sources in your project: |
| 36 | |
Thomas Van Lenten | 07f0231 | 2018-02-07 10:39:13 -0500 | [diff] [blame] | 37 | Add `objectivec/*.h`, `objectivec/google/protobuf/*.pbobjc.h`, and |
| 38 | `objectivec/GPBProtocolBuffers.m` to your project. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 39 | |
| 40 | *or* |
| 41 | |
Thomas Van Lenten | 07f0231 | 2018-02-07 10:39:13 -0500 | [diff] [blame] | 42 | Add `objectivec/*.h`, `objectivec/google/protobuf/*.pbobjc.h`, |
| 43 | `objectivec/google/protobuf/*.pbobjc.m`, and `objectivec/*.m` except for |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 44 | `objectivec/GPBProtocolBuffers.m` to your project. |
| 45 | |
| 46 | |
| 47 | If the target is using ARC, remember to turn off ARC (`-fno-objc-arc`) for the |
| 48 | `.m` files. |
| 49 | |
Thomas Van Lenten | 07f0231 | 2018-02-07 10:39:13 -0500 | [diff] [blame] | 50 | The files generated by `protoc` for the `*.proto` files (`*.pbobjc.h` and |
| 51 | `*.pbobjc.m`) are then also added to the target. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 52 | |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 53 | Usage |
| 54 | ----- |
| 55 | |
| 56 | The objects generated for messages should work like any other Objective C |
| 57 | object. They are mutable objects, but if you don't change them, they are safe |
| 58 | to share between threads (similar to passing an NSMutableDictionary between |
| 59 | threads/queues; as long as no one mutates it, things are fine). |
| 60 | |
| 61 | There are a few behaviors worth calling out: |
| 62 | |
| 63 | A property that is type NSString\* will never return nil. If the value is |
| 64 | unset, it will return an empty string (@""). This is inpart to align things |
| 65 | with the Protocol Buffers spec which says the default for strings is an empty |
| 66 | string, but also so you can always safely pass them to isEqual:/compare:, etc. |
| 67 | and have deterministic results. |
| 68 | |
| 69 | A property that is type NSData\* also won't return nil, it will return an empty |
| 70 | data ([NSData data]). The reasoning is the same as for NSString not returning |
| 71 | nil. |
| 72 | |
| 73 | A property that is another GPBMessage class also will not return nil. If the |
| 74 | field wasn't already set, you will get a instance of the correct class. This |
| 75 | instance will be a temporary instance unless you mutate it, at which point it |
| 76 | will be attached to its parent object. We call this pattern *autocreators*. |
| 77 | Similar to NSString and NSData properties it makes things a little safer when |
| 78 | using them with isEqual:/etc.; but more importantly, this allows you to write |
| 79 | code that uses Objective C's property dot notation to walk into nested objects |
| 80 | and access and/or assign things without having to check that they are not nil |
| 81 | and create them each step along the way. You can write this: |
| 82 | |
| 83 | ``` |
| 84 | - (void)updateRecord:(MyMessage *)msg { |
| 85 | ... |
| 86 | // Note: You don't have to check subMessage and otherMessage for nil and |
| 87 | // alloc/init/assign them back along the way. |
| 88 | msg.subMessage.otherMessage.lastName = @"Smith"; |
| 89 | ... |
| 90 | } |
| 91 | ``` |
| 92 | |
| 93 | If you want to check if a GPBMessage property is present, there is always as |
| 94 | `has\[NAME\]` property to go with the main property to check if it is set. |
| 95 | |
| 96 | A property that is of an Array or Dictionary type also provides *autocreator* |
| 97 | behavior and will never return nil. This provides all the same benefits you |
| 98 | see for the message properties. Again, you can write: |
| 99 | |
| 100 | ``` |
| 101 | - (void)updateRecord:(MyMessage *)msg { |
| 102 | ... |
| 103 | // Note: Just like above, you don't have to check subMessage and otherMessage |
| 104 | // for nil and alloc/init/assign them back along the way. You also don't have |
| 105 | // to create the siblingsArray, you can safely just append to it. |
| 106 | [msg.subMessage.otherMessage.siblingsArray addObject:@"Pat"]; |
| 107 | ... |
| 108 | } |
| 109 | ``` |
| 110 | |
| 111 | If you are inspecting a message you got from some other place (server, disk, |
| 112 | etc), you may want to check if the Array or Dictionary has entries without |
| 113 | causing it to be created for you. For this, there is always a `\[NAME\]_Count` |
| 114 | property also provided that can return zero or the real count, but won't trigger |
| 115 | the creation. |
| 116 | |
| 117 | For primitive type fields (ints, floats, bools, enum) in messages defined in a |
| 118 | `.proto` file that use *proto2* syntax there are conceptual differences between |
| 119 | having an *explicit* and *default* value. You can always get the value of the |
| 120 | property. In the case that it hasn't been set you will get the default. In |
| 121 | cases where you need to know whether it was set explicitly or you are just |
| 122 | getting the default, you can use the `has\[NAME\]` property. If the value has |
| 123 | been set, and you want to clear it, you can set the `has\[NAME\]` to `NO`. |
| 124 | *proto3* syntax messages do away with this concept, thus the default values are |
| 125 | never included when the message is encoded. |
| 126 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 127 | The Objective C classes/enums can be used from Swift code. |
| 128 | |
Thomas Van Lenten | a2a3399 | 2016-06-14 13:23:37 -0400 | [diff] [blame] | 129 | Objective C Generator Proto File Options |
| 130 | ---------------------------------------- |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 131 | |
| 132 | **objc_class_prefix=\<prefix\>** (no default) |
| 133 | |
| 134 | Since Objective C uses a global namespace for all of its classes, there can |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 135 | be collisions. This option provides a prefix that will be added to the Enums |
| 136 | and Objects (for messages) generated from the proto. Convention is to base |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 137 | the prefix on the package the proto is in. |
| 138 | |
Thomas Van Lenten | a2a3399 | 2016-06-14 13:23:37 -0400 | [diff] [blame] | 139 | Objective C Generator `protoc` Options |
| 140 | -------------------------------------- |
| 141 | |
| 142 | When generating Objective C code, `protoc` supports a `--objc_opt` argument; the |
| 143 | argument is comma-delimited name/value pairs (_key=value,key2=value2_). The |
| 144 | _keys_ are used to change the behavior during generation. The currently |
| 145 | supported keys are: |
| 146 | |
Nathan Wong | 3be6110 | 2016-06-27 22:56:34 +0100 | [diff] [blame] | 147 | * `generate_for_named_framework`: The `value` used for this key will be used |
Thomas Van Lenten | a2a3399 | 2016-06-14 13:23:37 -0400 | [diff] [blame] | 148 | when generating the `#import` statements in the generated code. Instead |
| 149 | of being plain `#import "some/path/file.pbobjc.h"` lines, they will be |
| 150 | framework based, i.e. - `#import <VALUE/file.pbobjc.h>`. |
| 151 | |
Thomas Van Lenten | 8c20e55 | 2016-06-17 10:31:05 -0400 | [diff] [blame] | 152 | _NOTE:_ If this is used with `named_framework_to_proto_path_mappings_path`, |
| 153 | then this is effectively the _default_ to use for everything that wasn't |
| 154 | mapped by the other. |
| 155 | |
| 156 | * `named_framework_to_proto_path_mappings_path`: The `value` used for this key |
| 157 | is a path to a file containing the listing of framework names and proto |
| 158 | files. The generator uses this to decide if another proto file referenced |
| 159 | should use a framework style import vs. a user level import |
| 160 | (`#import <FRAMEWORK/file.pbobjc.h>` vs `#import "dir/file.pbobjc.h"`). |
| 161 | |
| 162 | The format of the file is: |
| 163 | * An entry is a line of `frameworkName: file.proto, dir/file2.proto`. |
| 164 | * Comments start with `#`. |
| 165 | * A comment can go on a line after an entry. |
| 166 | (i.e. - `frameworkName: file.proto # comment`) |
| 167 | |
| 168 | Any number of files can be listed for a framework, just separate them with |
| 169 | commas. |
| 170 | |
Peter Newman | e2cc2de | 2020-08-10 19:08:25 +0100 | [diff] [blame] | 171 | There can be multiple lines listing the same frameworkName in case it has a |
Thomas Van Lenten | 8c20e55 | 2016-06-17 10:31:05 -0400 | [diff] [blame] | 172 | lot of proto files included in it; and having multiple lines makes things |
| 173 | easier to read. |
| 174 | |
Thomas Van Lenten | 9f546ba | 2020-06-09 11:19:15 -0400 | [diff] [blame] | 175 | * `runtime_import_prefix`: The `value` used for this key to be used as a |
| 176 | prefix on `#import`s of runtime provided headers in the generated files. |
| 177 | When integrating ObjC protos into a build system, this can be used to avoid |
| 178 | having to add the runtime directory to the header search path since the |
| 179 | generate `#import` will be more complete. |
| 180 | |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 181 | Contributing |
| 182 | ------------ |
| 183 | |
| 184 | Please make updates to the tests along with changes. If just changing the |
Thomas Van Lenten | d846b0b | 2015-06-08 16:24:57 -0400 | [diff] [blame] | 185 | runtime, the Xcode projects can be used to build and run tests. If your change |
| 186 | also requires changes to the generated code, |
| 187 | `objectivec/DevTools/full_mac_build.sh` can be used to easily rebuild and test |
| 188 | changes. Passing `-h` to the script will show the addition options that could |
| 189 | be useful. |
Thomas Van Lenten | 1dcc329 | 2015-05-21 17:14:52 -0400 | [diff] [blame] | 190 | |
| 191 | Documentation |
| 192 | ------------- |
| 193 | |
| 194 | The complete documentation for Protocol Buffers is available via the |
| 195 | web at: |
| 196 | |
| 197 | https://developers.google.com/protocol-buffers/ |