Back to Swiftgen

Objc

Documentation/templates/strings/objc.md

6.6.32.5 KB
Original Source

Template Information

NameDescription
File namesstrings/objc-string-h.stencil & strings/objc-string-m.stencil
Configuration example<pre>strings:
inputs: path/to/Localizable.strings
outputs:
- templateName: objc-h
  output: Localizable.h
- templateName: objc-m
  output: Localizable.m</pre> |

| Language | Objective-C | | Author | Eric Slosser |

When to use it

  • When you need to generate Objective-C code. Perhaps because you have a pure Objective-C project and don't want to add a Swift file to it (thus triggering the inclusion of the Swift runtime into your build). Or perhaps because you can't find a template that generates Swift that can be accessed from Objective-C.

Customization

You can customize some elements of this template by overriding the following parameters when invoking swiftgen. See the dedicated documentation.

Parameter NameDefault ValueDescription
bundle[NSBundle bundleForClass:BundleToken.class]Allows you to set from which bundle strings are loaded from. By default, it'll point to the same bundle as where the generated code is. Note: ignored if lookupFunction parameter is set.
headerName"Localizable.h"The name of the ObjC header generated by the objc-string-h template. The generated .m file will #include "{{headerName}}". This parameter should have the same value as the output file name used for the template: objc-string-h entry in your config file.
noCommentsN/ASetting this parameter will disable the comments containing the comment from the strings file or the translation of a key.

Generated Code

Extract:

swift
@interface Localizable : NSObject
/// Some alert body there
+ (NSString*)alertMessage;
/// Title for an alert
+ (NSString*)alertTitle;
/// These are %3$@'s %1$d %2$@.
+ (NSString*)objectOwnershipWithValues:(NSInteger)p1 :(id)p2 :(id)p3;
/// This is a %% character.
+ (NSString*)percent;
...
@end

Full generated code

Usage example

objc
// simple strings
NSString* message = Localizable.alertMessage
NSString* title = Localizable.alertTitle

// with parameters, note that each argument needs to be of the correct type
NSString* apples = [Localizable.applesCountWithValue:3];
NSString* bananas = [Localizable.bananasOwnerWithValues:5 :@"Olivier"];