Format Code Now
/
ConvertYAML to Objective-C Next: YAML to Python ›

YAML to Objective-C Class Generator

CV-92

Paste a YAML document and press Convert to get model classes with initWithDictionary:. The YAML is read into JSON first, so the types come from its values, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

YAML

Objective-C

How to use it

  1. Paste one YAML document, or open a .yaml or .yml file. Use a sample that shows every section you expect.
  2. Pick a class prefix such as XX, or none.
  3. Press Convert, then copy the result or download it as a file.
  4. Parse with NSJSONSerialization, then call [[Root alloc] initWithDictionary:dictionary]. The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.

How the classes are built

Each object becomes an NSObject subclass with properties, an initWithDictionary: initializer and a dictionary method that writes it back. Whole numbers use NSInteger, or long long beyond 32 bits, decimals use double and booleans use BOOL. Names that clash with C words, NSObject methods or ARC naming rules are changed, so id becomes idValue and newName becomes theNewName; a class prefix can be added. The output has a header part and an implementation part, split by a comment line, so copy each into its own .h or .m file.

How the YAML becomes classes

.nan and .inf become null. The YAML is parsed with the core schema, converted to JSON, and the code is built from that JSON. true/false, null, integers and floats get their usual types, while dates stay strings. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the generated types. Keys that are not valid Objective-C names, such as first-name, are renamed, and the original key is kept in the dictionary key used in initWithDictionary: and dictionary.

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay NSString; convert them with NSDateFormatter if you need NSDate values. A YAML file with optional sections should be pasted with every section present, or the missing sections will not appear in the output.

Questions people ask

How do I convert YAML to an Objective-C class?

Paste the YAML into the input box, choose your options and press Convert to get the classes. Parse with NSJSONSerialization, then call [[Root alloc] initWithDictionary:dictionary]. The classes follow the JSON form of the YAML. The conversion happens in your browser.

Why is an Objective-C property named idValue or theNewName?

Some names clash with C words, NSObject methods or ARC naming rules. Keys such as id, class and description get a Value suffix, and names starting with new, copy, init or alloc get a the prefix. The original key is still used when reading and writing. This applies to the JSON form of your YAML.

What does the class prefix option do in the Objective-C output?

Objective-C has no namespaces, so apps often prefix class names. Pick XX, API or App and every generated class name gets it, for example XXOrder. The default adds no prefix. This applies to the JSON form of your YAML.

Where do the header and implementation go in the Objective-C output?

The output holds both, split by two comment lines. Copy the part under the header comment into a .h file and the part under the implementation comment into a .m file, then import the header wherever you use the classes. This applies to the JSON form of your YAML.

Why did a YAML value like 1.0 come out as an integer type?

The YAML is converted to JSON before types are chosen, and 1.0 is the same number as 1 there. A field that always holds whole-looking decimals in your sample can come out as an integer type. Add a non-whole decimal to the sample, or change the type by hand. The Objective-C classes are written from that converted data.

Can I paste a YAML file with several documents?

No. Only a single document is read; a file with --- separators between documents gives an error. Paste one document at a time, or merge them into a list under one top-level key yourself. The Objective-C classes are written from that converted data.

About this tool

The YAML is read with the core schema (true/false, null, integers and floats), converted to JSON, and the code is generated from that. Dates stay text, comments are dropped, anchors, aliases and << merge keys are resolved, keys that are not text become text, and only one document is read. A value such as 1.0 is read as the number 1, so a field that holds whole-looking decimals can come out as an integer type; an integer beyond 9007199254740991 is rounded by the YAML reader before any code is written; .nan and .inf become null. Every object becomes an NSObject subclass with properties, an initWithDictionary: initializer that checks each value with isKindOfClass: before using it, and a dictionary method that writes the object back. The output holds a header section and an implementation section, separated by a comment line. Strings are NSString, arrays are NSArray with lightweight generics, nested objects are their own classes, and numbers use NSInteger, long long, double or BOOL, with NSNumber for keys that are null or missing in some items. Names that clash with C, Objective-C or ARC naming rules get a suffix or prefix, for example id becomes idValue. Values with the wrong type, and list items that fail to convert, are skipped rather than raising an error. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.