1*1b3f573fSAndroid Build Coastguard WorkerProtocol Buffers - Google's data interchange format 2*1b3f573fSAndroid Build Coastguard Worker=================================================== 3*1b3f573fSAndroid Build Coastguard Worker 4*1b3f573fSAndroid Build Coastguard WorkerCopyright 2008 Google Inc. 5*1b3f573fSAndroid Build Coastguard Worker 6*1b3f573fSAndroid Build Coastguard WorkerThis directory contains the Objective C Protocol Buffers runtime library. 7*1b3f573fSAndroid Build Coastguard Worker 8*1b3f573fSAndroid Build Coastguard WorkerRequirements 9*1b3f573fSAndroid Build Coastguard Worker------------ 10*1b3f573fSAndroid Build Coastguard Worker 11*1b3f573fSAndroid Build Coastguard WorkerThe Objective C implementation requires: 12*1b3f573fSAndroid Build Coastguard Worker 13*1b3f573fSAndroid Build Coastguard Worker- Objective C 2.0 Runtime (32bit & 64bit iOS, 64bit OS X). 14*1b3f573fSAndroid Build Coastguard Worker- Xcode 10.3 (or later). 15*1b3f573fSAndroid Build Coastguard Worker- The library code does *not* use ARC (for performance reasons), but it all can 16*1b3f573fSAndroid Build Coastguard Worker be called from ARC code. 17*1b3f573fSAndroid Build Coastguard Worker 18*1b3f573fSAndroid Build Coastguard WorkerInstallation 19*1b3f573fSAndroid Build Coastguard Worker------------ 20*1b3f573fSAndroid Build Coastguard Worker 21*1b3f573fSAndroid Build Coastguard WorkerThe distribution pulled from github includes the sources for both the 22*1b3f573fSAndroid Build Coastguard Workercompiler (protoc) and the runtime (this directory). After cloning the distribution 23*1b3f573fSAndroid Build Coastguard Workerand needed submodules ([see the src directory's README](../src/README.md)), 24*1b3f573fSAndroid Build Coastguard Workerto build the compiler and run the runtime tests, you can use: 25*1b3f573fSAndroid Build Coastguard Worker 26*1b3f573fSAndroid Build Coastguard Worker $ objectivec/DevTools/full_mac_build.sh 27*1b3f573fSAndroid Build Coastguard Worker 28*1b3f573fSAndroid Build Coastguard WorkerThis will generate the `src/protoc` binary. 29*1b3f573fSAndroid Build Coastguard Worker 30*1b3f573fSAndroid Build Coastguard WorkerBuilding 31*1b3f573fSAndroid Build Coastguard Worker-------- 32*1b3f573fSAndroid Build Coastguard Worker 33*1b3f573fSAndroid Build Coastguard WorkerThere are two ways to include the Runtime sources in your project: 34*1b3f573fSAndroid Build Coastguard Worker 35*1b3f573fSAndroid Build Coastguard WorkerAdd `objectivec/*.h`, `objectivec/google/protobuf/*.pbobjc.h`, and 36*1b3f573fSAndroid Build Coastguard Worker`objectivec/GPBProtocolBuffers.m` to your project. 37*1b3f573fSAndroid Build Coastguard Worker 38*1b3f573fSAndroid Build Coastguard Worker*or* 39*1b3f573fSAndroid Build Coastguard Worker 40*1b3f573fSAndroid Build Coastguard WorkerAdd `objectivec/*.h`, `objectivec/google/protobuf/*.pbobjc.h`, 41*1b3f573fSAndroid Build Coastguard Worker`objectivec/google/protobuf/*.pbobjc.m`, and `objectivec/*.m` except for 42*1b3f573fSAndroid Build Coastguard Worker`objectivec/GPBProtocolBuffers.m` to your project. 43*1b3f573fSAndroid Build Coastguard Worker 44*1b3f573fSAndroid Build Coastguard Worker 45*1b3f573fSAndroid Build Coastguard WorkerIf the target is using ARC, remember to turn off ARC (`-fno-objc-arc`) for the 46*1b3f573fSAndroid Build Coastguard Worker`.m` files. 47*1b3f573fSAndroid Build Coastguard Worker 48*1b3f573fSAndroid Build Coastguard WorkerThe files generated by `protoc` for the `*.proto` files (`*.pbobjc.h` and 49*1b3f573fSAndroid Build Coastguard Worker`*.pbobjc.m`) are then also added to the target. 50*1b3f573fSAndroid Build Coastguard Worker 51*1b3f573fSAndroid Build Coastguard WorkerUsage 52*1b3f573fSAndroid Build Coastguard Worker----- 53*1b3f573fSAndroid Build Coastguard Worker 54*1b3f573fSAndroid Build Coastguard WorkerThe objects generated for messages should work like any other Objective C 55*1b3f573fSAndroid Build Coastguard Workerobject. They are mutable objects, but if you don't change them, they are safe 56*1b3f573fSAndroid Build Coastguard Workerto share between threads (similar to passing an NSMutableDictionary between 57*1b3f573fSAndroid Build Coastguard Workerthreads/queues; as long as no one mutates it, things are fine). 58*1b3f573fSAndroid Build Coastguard Worker 59*1b3f573fSAndroid Build Coastguard WorkerThere are a few behaviors worth calling out: 60*1b3f573fSAndroid Build Coastguard Worker 61*1b3f573fSAndroid Build Coastguard WorkerA property that is type NSString\* will never return nil. If the value is 62*1b3f573fSAndroid Build Coastguard Workerunset, it will return an empty string (@""). This is inpart to align things 63*1b3f573fSAndroid Build Coastguard Workerwith the Protocol Buffers spec which says the default for strings is an empty 64*1b3f573fSAndroid Build Coastguard Workerstring, but also so you can always safely pass them to isEqual:/compare:, etc. 65*1b3f573fSAndroid Build Coastguard Workerand have deterministic results. 66*1b3f573fSAndroid Build Coastguard Worker 67*1b3f573fSAndroid Build Coastguard WorkerA property that is type NSData\* also won't return nil, it will return an empty 68*1b3f573fSAndroid Build Coastguard Workerdata ([NSData data]). The reasoning is the same as for NSString not returning 69*1b3f573fSAndroid Build Coastguard Workernil. 70*1b3f573fSAndroid Build Coastguard Worker 71*1b3f573fSAndroid Build Coastguard WorkerA property that is another GPBMessage class also will not return nil. If the 72*1b3f573fSAndroid Build Coastguard Workerfield wasn't already set, you will get a instance of the correct class. This 73*1b3f573fSAndroid Build Coastguard Workerinstance will be a temporary instance unless you mutate it, at which point it 74*1b3f573fSAndroid Build Coastguard Workerwill be attached to its parent object. We call this pattern *autocreators*. 75*1b3f573fSAndroid Build Coastguard WorkerSimilar to NSString and NSData properties it makes things a little safer when 76*1b3f573fSAndroid Build Coastguard Workerusing them with isEqual:/etc.; but more importantly, this allows you to write 77*1b3f573fSAndroid Build Coastguard Workercode that uses Objective C's property dot notation to walk into nested objects 78*1b3f573fSAndroid Build Coastguard Workerand access and/or assign things without having to check that they are not nil 79*1b3f573fSAndroid Build Coastguard Workerand create them each step along the way. You can write this: 80*1b3f573fSAndroid Build Coastguard Worker 81*1b3f573fSAndroid Build Coastguard Worker``` 82*1b3f573fSAndroid Build Coastguard Worker- (void)updateRecord:(MyMessage *)msg { 83*1b3f573fSAndroid Build Coastguard Worker ... 84*1b3f573fSAndroid Build Coastguard Worker // Note: You don't have to check subMessage and otherMessage for nil and 85*1b3f573fSAndroid Build Coastguard Worker // alloc/init/assign them back along the way. 86*1b3f573fSAndroid Build Coastguard Worker msg.subMessage.otherMessage.lastName = @"Smith"; 87*1b3f573fSAndroid Build Coastguard Worker ... 88*1b3f573fSAndroid Build Coastguard Worker} 89*1b3f573fSAndroid Build Coastguard Worker``` 90*1b3f573fSAndroid Build Coastguard Worker 91*1b3f573fSAndroid Build Coastguard WorkerIf you want to check if a GPBMessage property is present, there is always as 92*1b3f573fSAndroid Build Coastguard Worker`has\[NAME\]` property to go with the main property to check if it is set. 93*1b3f573fSAndroid Build Coastguard Worker 94*1b3f573fSAndroid Build Coastguard WorkerA property that is of an Array or Dictionary type also provides *autocreator* 95*1b3f573fSAndroid Build Coastguard Workerbehavior and will never return nil. This provides all the same benefits you 96*1b3f573fSAndroid Build Coastguard Workersee for the message properties. Again, you can write: 97*1b3f573fSAndroid Build Coastguard Worker 98*1b3f573fSAndroid Build Coastguard Worker``` 99*1b3f573fSAndroid Build Coastguard Worker- (void)updateRecord:(MyMessage *)msg { 100*1b3f573fSAndroid Build Coastguard Worker ... 101*1b3f573fSAndroid Build Coastguard Worker // Note: Just like above, you don't have to check subMessage and otherMessage 102*1b3f573fSAndroid Build Coastguard Worker // for nil and alloc/init/assign them back along the way. You also don't have 103*1b3f573fSAndroid Build Coastguard Worker // to create the siblingsArray, you can safely just append to it. 104*1b3f573fSAndroid Build Coastguard Worker [msg.subMessage.otherMessage.siblingsArray addObject:@"Pat"]; 105*1b3f573fSAndroid Build Coastguard Worker ... 106*1b3f573fSAndroid Build Coastguard Worker} 107*1b3f573fSAndroid Build Coastguard Worker``` 108*1b3f573fSAndroid Build Coastguard Worker 109*1b3f573fSAndroid Build Coastguard WorkerIf you are inspecting a message you got from some other place (server, disk, 110*1b3f573fSAndroid Build Coastguard Workeretc), you may want to check if the Array or Dictionary has entries without 111*1b3f573fSAndroid Build Coastguard Workercausing it to be created for you. For this, there is always a `\[NAME\]_Count` 112*1b3f573fSAndroid Build Coastguard Workerproperty also provided that can return zero or the real count, but won't trigger 113*1b3f573fSAndroid Build Coastguard Workerthe creation. 114*1b3f573fSAndroid Build Coastguard Worker 115*1b3f573fSAndroid Build Coastguard WorkerFor primitive type fields (ints, floats, bools, enum) in messages defined in a 116*1b3f573fSAndroid Build Coastguard Worker`.proto` file that use *proto2* syntax there are conceptual differences between 117*1b3f573fSAndroid Build Coastguard Workerhaving an *explicit* and *default* value. You can always get the value of the 118*1b3f573fSAndroid Build Coastguard Workerproperty. In the case that it hasn't been set you will get the default. In 119*1b3f573fSAndroid Build Coastguard Workercases where you need to know whether it was set explicitly or you are just 120*1b3f573fSAndroid Build Coastguard Workergetting the default, you can use the `has\[NAME\]` property. If the value has 121*1b3f573fSAndroid Build Coastguard Workerbeen set, and you want to clear it, you can set the `has\[NAME\]` to `NO`. 122*1b3f573fSAndroid Build Coastguard Worker*proto3* syntax messages do away with this concept, thus the default values are 123*1b3f573fSAndroid Build Coastguard Workernever included when the message is encoded. 124*1b3f573fSAndroid Build Coastguard Worker 125*1b3f573fSAndroid Build Coastguard WorkerThe Objective C classes/enums can be used from Swift code. 126*1b3f573fSAndroid Build Coastguard Worker 127*1b3f573fSAndroid Build Coastguard WorkerObjective C Generator Proto File Options 128*1b3f573fSAndroid Build Coastguard Worker---------------------------------------- 129*1b3f573fSAndroid Build Coastguard Worker 130*1b3f573fSAndroid Build Coastguard Worker**objc_class_prefix=\<prefix\>** (no default) 131*1b3f573fSAndroid Build Coastguard Worker 132*1b3f573fSAndroid Build Coastguard WorkerThis options allow you to provide a custom prefix for all the symbols generated 133*1b3f573fSAndroid Build Coastguard Workerfrom a proto file (classes (from message), enums, the Root for extension 134*1b3f573fSAndroid Build Coastguard Workersupport). 135*1b3f573fSAndroid Build Coastguard Worker 136*1b3f573fSAndroid Build Coastguard WorkerIf not set, the generation options `package_to_prefix_mappings_path` and 137*1b3f573fSAndroid Build Coastguard Worker`use_package_as_prefix` (documented below) controls what is used instead. Since 138*1b3f573fSAndroid Build Coastguard WorkerObjective C uses a global namespace for all of its classes, there can be collisions. 139*1b3f573fSAndroid Build Coastguard Worker`use_package_as_prefix=yes` should avoid collisions since proto package are used to 140*1b3f573fSAndroid Build Coastguard Workerscope/name things in other languages, but this option can be used to get shorter 141*1b3f573fSAndroid Build Coastguard Workernames instead. Convention is to base the explicit prefix on the proto package. 142*1b3f573fSAndroid Build Coastguard Worker 143*1b3f573fSAndroid Build Coastguard WorkerObjective C Generator `protoc` Options 144*1b3f573fSAndroid Build Coastguard Worker-------------------------------------- 145*1b3f573fSAndroid Build Coastguard Worker 146*1b3f573fSAndroid Build Coastguard WorkerWhen generating Objective C code, `protoc` supports a `--objc_opt` argument; the 147*1b3f573fSAndroid Build Coastguard Workerargument is comma-delimited name/value pairs (_key=value,key2=value2_). The 148*1b3f573fSAndroid Build Coastguard Worker_keys_ are used to change the behavior during generation. The currently 149*1b3f573fSAndroid Build Coastguard Workersupported keys are: 150*1b3f573fSAndroid Build Coastguard Worker 151*1b3f573fSAndroid Build Coastguard Worker * `generate_for_named_framework`: The `value` used for this key will be used 152*1b3f573fSAndroid Build Coastguard Worker when generating the `#import` statements in the generated code. Instead 153*1b3f573fSAndroid Build Coastguard Worker of being plain `#import "some/path/file.pbobjc.h"` lines, they will be 154*1b3f573fSAndroid Build Coastguard Worker framework based, i.e. - `#import <VALUE/file.pbobjc.h>`. 155*1b3f573fSAndroid Build Coastguard Worker 156*1b3f573fSAndroid Build Coastguard Worker _NOTE:_ If this is used with `named_framework_to_proto_path_mappings_path`, 157*1b3f573fSAndroid Build Coastguard Worker then this is effectively the _default_ to use for everything that wasn't 158*1b3f573fSAndroid Build Coastguard Worker mapped by the other. 159*1b3f573fSAndroid Build Coastguard Worker 160*1b3f573fSAndroid Build Coastguard Worker * `named_framework_to_proto_path_mappings_path`: The `value` used for this key 161*1b3f573fSAndroid Build Coastguard Worker is a path to a file containing the listing of framework names and proto 162*1b3f573fSAndroid Build Coastguard Worker files. The generator uses this to decide if another proto file referenced 163*1b3f573fSAndroid Build Coastguard Worker should use a framework style import vs. a user level import 164*1b3f573fSAndroid Build Coastguard Worker (`#import <FRAMEWORK/file.pbobjc.h>` vs `#import "dir/file.pbobjc.h"`). 165*1b3f573fSAndroid Build Coastguard Worker 166*1b3f573fSAndroid Build Coastguard Worker The format of the file is: 167*1b3f573fSAndroid Build Coastguard Worker * An entry is a line of `frameworkName: file.proto, dir/file2.proto`. 168*1b3f573fSAndroid Build Coastguard Worker * Comments start with `#`. 169*1b3f573fSAndroid Build Coastguard Worker * A comment can go on a line after an entry. 170*1b3f573fSAndroid Build Coastguard Worker (i.e. - `frameworkName: file.proto # comment`) 171*1b3f573fSAndroid Build Coastguard Worker 172*1b3f573fSAndroid Build Coastguard Worker Any number of files can be listed for a framework, just separate them with 173*1b3f573fSAndroid Build Coastguard Worker commas. 174*1b3f573fSAndroid Build Coastguard Worker 175*1b3f573fSAndroid Build Coastguard Worker There can be multiple lines listing the same frameworkName in case it has a 176*1b3f573fSAndroid Build Coastguard Worker lot of proto files included in it; and having multiple lines makes things 177*1b3f573fSAndroid Build Coastguard Worker easier to read. 178*1b3f573fSAndroid Build Coastguard Worker 179*1b3f573fSAndroid Build Coastguard Worker * `runtime_import_prefix`: The `value` used for this key to be used as a 180*1b3f573fSAndroid Build Coastguard Worker prefix on `#import`s of runtime provided headers in the generated files. 181*1b3f573fSAndroid Build Coastguard Worker When integrating ObjC protos into a build system, this can be used to avoid 182*1b3f573fSAndroid Build Coastguard Worker having to add the runtime directory to the header search path since the 183*1b3f573fSAndroid Build Coastguard Worker generate `#import` will be more complete. 184*1b3f573fSAndroid Build Coastguard Worker 185*1b3f573fSAndroid Build Coastguard Worker * `package_to_prefix_mappings_path`: The `value` used for this key is a 186*1b3f573fSAndroid Build Coastguard Worker path to a file containing a list of proto packages and prefixes. 187*1b3f573fSAndroid Build Coastguard Worker The generator will use this to locate which ObjC class prefix to use when 188*1b3f573fSAndroid Build Coastguard Worker generating sources _unless_ the `objc_class_prefix` file option is set. 189*1b3f573fSAndroid Build Coastguard Worker This option can be useful if multiple apps consume a common set of 190*1b3f573fSAndroid Build Coastguard Worker proto files but wish to use a different prefix for the generated sources 191*1b3f573fSAndroid Build Coastguard Worker between them. This option takes precedent over the `use_package_as_prefix` 192*1b3f573fSAndroid Build Coastguard Worker option. 193*1b3f573fSAndroid Build Coastguard Worker 194*1b3f573fSAndroid Build Coastguard Worker The format of the file is: 195*1b3f573fSAndroid Build Coastguard Worker * An entry is a line of "package=prefix". 196*1b3f573fSAndroid Build Coastguard Worker * Comments start with `#`. 197*1b3f573fSAndroid Build Coastguard Worker * A comment can go on a line after a expected package/prefix pair. 198*1b3f573fSAndroid Build Coastguard Worker (i.e. - "package=prefix # comment") 199*1b3f573fSAndroid Build Coastguard Worker * For files that do NOT have a proto package (not recommended), an 200*1b3f573fSAndroid Build Coastguard Worker entry can be made as "no_package:PATH=prefix", where PATH is the 201*1b3f573fSAndroid Build Coastguard Worker path for the .proto file. 202*1b3f573fSAndroid Build Coastguard Worker 203*1b3f573fSAndroid Build Coastguard Worker * `use_package_as_prefix` and `proto_package_prefix_exceptions_path`: The 204*1b3f573fSAndroid Build Coastguard Worker `value` for `use_package_as_prefix` can be `yes` or `no`, and indicates 205*1b3f573fSAndroid Build Coastguard Worker if a prefix should be derived from the proto package for all the symbols 206*1b3f573fSAndroid Build Coastguard Worker for files that don't have the `objc_class_prefix` file option (mentioned 207*1b3f573fSAndroid Build Coastguard Worker above). This helps ensure the symbols are more unique and means there is 208*1b3f573fSAndroid Build Coastguard Worker less chance of ObjC class name collisions. 209*1b3f573fSAndroid Build Coastguard Worker 210*1b3f573fSAndroid Build Coastguard Worker To help in migrating code to using this support, 211*1b3f573fSAndroid Build Coastguard Worker `proto_package_prefix_exceptions_path` can be used to provide the path 212*1b3f573fSAndroid Build Coastguard Worker to a file that contains proto package names (one per line, comments allowed 213*1b3f573fSAndroid Build Coastguard Worker if prefixed with `#`). These package won't get the derived prefix, allowing 214*1b3f573fSAndroid Build Coastguard Worker migrations to the behavior one proto package at a time across a code base. 215*1b3f573fSAndroid Build Coastguard Worker 216*1b3f573fSAndroid Build Coastguard Worker `use_package_as_prefix` currently defaults to `no` (existing behavior), but 217*1b3f573fSAndroid Build Coastguard Worker in the future (as a breaking change), that is likely to change since it 218*1b3f573fSAndroid Build Coastguard Worker helps prepare folks before they end up using a lot of protos and getting a 219*1b3f573fSAndroid Build Coastguard Worker lot of collisions. 220*1b3f573fSAndroid Build Coastguard Worker 221*1b3f573fSAndroid Build Coastguard Worker * `headers_use_forward_declarations`: The `value` for this can be `yes` or 222*1b3f573fSAndroid Build Coastguard Worker `no`, and indicates if the generated headers use forward declarations for 223*1b3f573fSAndroid Build Coastguard Worker Message and Enum types from other .proto files or if the files should be 224*1b3f573fSAndroid Build Coastguard Worker imported into the generated header instead. 225*1b3f573fSAndroid Build Coastguard Worker 226*1b3f573fSAndroid Build Coastguard Worker By using forward declarations, less code is likely to recompile when the 227*1b3f573fSAndroid Build Coastguard Worker files do change, but Swift generally doesn't like forward declarations and 228*1b3f573fSAndroid Build Coastguard Worker will fail to include properties when the concrete definition of the type is 229*1b3f573fSAndroid Build Coastguard Worker known at import time. If your proto usages span modules, this can be a 230*1b3f573fSAndroid Build Coastguard Worker problem. 231*1b3f573fSAndroid Build Coastguard Worker 232*1b3f573fSAndroid Build Coastguard Worker `headers_use_forward_declarations` currently defaults to `yes` (existing 233*1b3f573fSAndroid Build Coastguard Worker behavior), but in a future release, that default may change to provide 234*1b3f573fSAndroid Build Coastguard Worker better Swift support by default. 235*1b3f573fSAndroid Build Coastguard Worker 236*1b3f573fSAndroid Build Coastguard WorkerContributing 237*1b3f573fSAndroid Build Coastguard Worker------------ 238*1b3f573fSAndroid Build Coastguard Worker 239*1b3f573fSAndroid Build Coastguard WorkerPlease make updates to the tests along with changes. If just changing the 240*1b3f573fSAndroid Build Coastguard Workerruntime, the Xcode projects can be used to build and run tests. If your change 241*1b3f573fSAndroid Build Coastguard Workeralso requires changes to the generated code, 242*1b3f573fSAndroid Build Coastguard Worker`objectivec/DevTools/full_mac_build.sh` can be used to easily rebuild and test 243*1b3f573fSAndroid Build Coastguard Workerchanges. Passing `-h` to the script will show the addition options that could 244*1b3f573fSAndroid Build Coastguard Workerbe useful. 245*1b3f573fSAndroid Build Coastguard Worker 246*1b3f573fSAndroid Build Coastguard WorkerDocumentation 247*1b3f573fSAndroid Build Coastguard Worker------------- 248*1b3f573fSAndroid Build Coastguard Worker 249*1b3f573fSAndroid Build Coastguard WorkerThe complete documentation for Protocol Buffers is available via the 250*1b3f573fSAndroid Build Coastguard Workerweb at: 251*1b3f573fSAndroid Build Coastguard Worker 252*1b3f573fSAndroid Build Coastguard Workerhttps://developers.google.com/protocol-buffers/ 253