Learning Center

Learning Center

Learn Menu

Overview

The Swift engine supports several variants:

  • Basic: Generates models as struct/class with Codable conformance.
  • Realm: Generates Realm models ready for storage using Realm Database.
  • Core Data: Generates Core Data models including the xcdatamodel file.

Xcode Project File

Unlike other IDEs, adding a new directory/file to your project path won't automatically add it to an Xcode project. Xcode project file keeps track of all the added files, so you need to manually manage files in your Xcode project. Because of this behavior, you should always re-add the target directory (the one where the Swift OneGen models get generated in) as a model could have been added/removed/renamed.

Data Types

Standard

Design Swift
bool Bool
int Int
int8 Int8
int16 Int32
int32 Int32
int64 Int64
uint UInt
uint8 UInt8
uint16 UInt32
uint32 UInt32
uint64 UInt64
float Float
double Double
string String
char String
byte UInt8
bytea Data
date Date
datetime Date

Complex

Name Swift Type
Array Array, e.g. [Int]
Map Dictionary, e.g. [String:Int]

Date Formatting

Since OneGen's raw date/time representation is the ISO 8601 as described here, you need to set the decoding/encoding strategy to ISO 8601 as shown below.

JSON Decoding

  let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601

JSON Encoding

  let encoder = JSONEncoder()
    encoder.dateEncodingStrategy = .iso8601

Limitations of Open Access Level

If you set open access level, you may encounter that some models get generated with public access level instead. That's because swift doesn't allow to mark open the following:

  • Enums can't be marked open.
  • Static Methods: Used for static validation methods
  • Convenience Constructor: Used for Core Data's Codable conformance
  • Typealias: Used for a nested entities in Core Data and Realm frameworks as nested entities need to be flattened

Nested Entities

Nested classes/enums is a feature that Swift supports just fine. When it comes to storage, it's another story. It's not very typical to store nested entities, the database design is usually flat, thus neither Realm or Core Data support it.

OneGen's solution to this problem is flattening the nested entities. We basically take a nested entity, move it out of its parent class(es) and rename it by prepending the parent(s)'s names.

Example

  class Animal {
    class Dog {
    }
}

OneGen would flatten the Dog entity like this:

  class AnimalDog {
}

class Animal {
    // Notice this convenient alias
    typealias Dog = AnimalDog
}

See the typealias? OneGen leaves a typealias behind, so you can still access the Dog as if it were nested:

  let myDog = Animal.Dog() // works!

We use cookies to track activity using Google Analytics & reCAPTCHA. It helps us understand what our visitors like about our product and how they interact with our website. For more information, check out our Privacy Policy.