Latest 4.0.1
License MIT
Platforms ios 8.0
Dependencies ObjectMapper
Frameworks Foundation, UIKit

Carthage compatible
CI Status

  • Adds simple calls to include NULL values in output JSON.
  • Adds ability to simply type cast JSON values to specified type.
  • Adds ability to map Swift base type arrays into Realm arrays.
  • Adds TimestampTransform to simply transform to/from UNIX timestamps.
  • Adds ISO8601JustDateTransform to simplty transform to/from ISO8601 date string. Because ObjectMapper’s ISO8601DateTransform actually is date and time transform.


To run the example project, clone the repo, and run pod install from the Example directory first.



Please check official guide


github "APUtils/ObjectMapperAdditions"

If you do not need Realm part, add those frameworks: ObjectMapperAdditions, ObjectMapper.

If you are going to use Realm part, add those frameworks: ObjectMapperAdditions, ObjectMapperAdditionsRealm, ObjectMapper, Realm, RealmSwift.


ObjectMapperAdditions is available through CocoaPods.

To install Core features, simply add the following line to your Podfile:

pod 'ObjectMapperAdditions/Core'

To add Realm transform to your project add the following line to your Podfile:

pod 'ObjectMapperAdditions/Realm'


Core Features

It’s a common case when app gets Int in JSON instead of String even if backend guy said you it’ll be String. Worst of all sometimes it could be String and sometimes something else so it’ll look like you released broken app even if you tested it well.

After several projects I made a rule for myself: Never trust a backend!. I always make optional fields and cast values to type I’ll use. Right now I’m using a great framework ObjectMapper to map my objects but it doesn’t have transforms I need so I wrote them as this separate pod.

Example model:

import Foundation
import ObjectMapper
import ObjectMapperAdditions

struct MyModel: Mappable {
    var string: String?
    var stringsArray: [String]?
    var double: Double?
    var myOtherModel: MyOtherModel?
    var myOtherModelsArray: [MyOtherModel]?

    init?(map: Map) {}

    mutating func mapping(map: Map) {
        // You could specify proper type transform directly
        string <- (map["string"], StringTransform())

        // Or you could just use TypeCastTransform
        string <- (map["string"], TypeCastTransform())

        // No doubt it also works with Double
        double <- (map["double"], TypeCastTransform())

        // Works with arrays too but for TypeCastTransform you must specify type
        stringsArray <- (map["stringsArray"], TypeCastTransform<String>())

        // Or just use StringTransform directly
        stringsArray <- (map["stringsArray"], StringTransform())

        // No need to transform your types. They should specify transforms by themselfs.
        myOtherModel <- map["myOtherModel"]
        myOtherModelsArray <- map["myOtherModelsArray"]

Right now there are 4 base type transforms you could use: BoolTransform, DoubleTransform, IntTransform and StringTransform. But for basic types it’s easier to just use TypeCastTransform which will type cast to proper type automatically.

Moreover this pod has extension to simplify creation of JSON with NULL values included from objects. Just call .toJSON(shouldIncludeNilValues: true) on BaseMappable object or array/set.

Date transformers example usage:

// If date in timestamp format (1506423767)
date <- (map["date"], TimestampTransform())

// If date in ISO8601 full-date format (yyyy-MM-dd)
date <- (map["date"], ISO8601JustDateTransform())

See example and tests projects for more details.

Realm Features

This part of ObjectMapperAdditions solves issues that prevent simply using ObjectMapper and Realm in one model. There is already ObjectMapper-Realm framework which allows to transform arrays of custom type to realm lists, but it can’t transform simple type arrays.

import Foundation
import ObjectMapper
import ObjectMapperAdditions
import ObjectMapper_Realm
import RealmSwift

class MyRealmModel: Object, Mappable {
    @objc dynamic var double: Double = 0
    @objc dynamic var string: String?
    @objc dynamic var myOtherRealmModel: MyOtherRealmModel?

    // Please take a note it's `var` and is not optional
    var myOtherRealmModels: List<MyOtherRealmModel> = List<MyOtherRealmModel>()

    // Strings array will be casted to List<RealmString>
    var strings: List<String> = List<String>()

    required convenience init?(map: Map) { self.init() }

    func mapping(map: Map) {
        // .toJSON() requires Realm write transaction or it'll crash
        let isWriteRequired = realm != nil && realm?.isInWriteTransaction == false
        isWriteRequired ? realm?.beginWrite() : ()

        // Same as for ordinary model
        double <- (map["double"], DoubleTransform())
        string <- (map["string"], StringTransform())
        myOtherRealmModel <- map["myOtherRealmModel"]

        // Using ObjectMapper+Realm's ListTransform to transform custom types
        myOtherRealmModels <- (map["myOtherRealmModels"], ListTransform<MyOtherRealmModel>())

        // Using ObjectMapperAdditions's RealmTypeCastTransform
        strings <- (map["strings"], RealmTypeCastTransform())

        // You could also use RealmTransform if you don't like type cast
//        strings <- (map["strings"], RealmTransform())

        isWriteRequired ? try? realm?.commitWrite() : ()

Swift arrays cast to realm arrays this way: [String] -> List<String>, [Int] -> List<String>, [Double] -> List<Double>, [Bool] -> List<Bool>, etc.

Be sure to check that properties of type List are not dynamic and not optional. Also despite of they defined as var they should be handled as constants if model is added to Realm. Use .removeAll() and append(objectsIn:) methods to change List content

See example and tests projects for more details.


Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.


Anton Plebanovich, [email protected]


ObjectMapperAdditions is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "ObjectMapperAdditions",
    "version": "4.0.1",
    "summary": "ObjectMapper Extensions and Transforms",
    "description": "- Adds simple calls to include NULL values in output JSON.n- Adds ability to simply type cast JSON values to specified type.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Anton Plebanovich": "[email protected]"
    "source": {
        "git": "",
        "tag": "4.0.1"
    "platforms": {
        "ios": "8.0"
    "source_files": "ObjectMapperAdditions/Classes/**/*",
    "frameworks": [
    "dependencies": {
        "ObjectMapper": []
    "subspecs": [
            "name": "Core",
            "source_files": "ObjectMapperAdditions/Classes/Core/**/*"
            "name": "Realm",
            "source_files": "ObjectMapperAdditions/Classes/Realm/**/*",
            "dependencies": {
                "ObjectMapperAdditions/Core": [],
                "RealmSwift": []

Pin It on Pinterest

Share This