• Stars
    star
    242
  • Rank 167,048 (Top 4 %)
  • Language
    Swift
  • License
    Other
  • Created over 9 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ‘”A super fast & convenient object mapper tailored for your needs

Tailor Swift logo

CI Status Version Carthage Compatible License Platform Documentation Swift

A super fast & convenient object mapper tailored for your needs.

Mapping objects to arrays or dictionaries can be a really cumbersome task, but those days are over. Tailor features a whole bunch of nifty methods for your model sewing needs.

Mapping properties

Tailor features property, relation(s) mapping for both struct and class objects.

Struct

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
  }
}

let dictionary = ["first_name" : "Taylor", "last_name" : "Swift"]
let model = Person(dictionary)

Class

class Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""

  required convenience init(_ map: [String : AnyObject]) {
    self.init()
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
  }
}

let dictionary = ["first_name" : "Taylor", "last_name" : "Swift"]
let model = Person(dictionary)

Mapping objects

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?
  var parents = [Person]()

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.relation("spouse")
    parents   <- map.relations("parents")
  }
}

let dictionary = [
  "first_name" : "Taylor",
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin",
              "last_name" : "Harris"],
  "parents" : [
             ["first_name" : "Andrea",
              "last_name" : "Swift"],
              ["first_name" : "Scott",
              "last_name" : "Swift"]
  ]
]
let model = Person(dictionary)

SafeMappable

struct ImmutablePerson: SafeMappable {
  let firstName: String
  let lastName: String
  let spouse: Person
  let parents = [Person]()

  init(_ map: [String : Any]) throws {
    firstName = try map.property("firstName").unwrapOrThrow()
    lastName = try map.property("lastName").unwrapOrThrow()
    spouse = try map.relationOrThrow("spouse").unwrapOrThrow()
    parents = try map.relationsOrThrow("parents").unwrapOrThrow()
  }
}

let immutablePerson: ImmutablePerson
do {
  immutablePerson = try TestImmutable(["firstName" : "foo" , "lastName" : "bar"])
} catch {
  print(error)
}

Transforms

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?
  var parents = [Person]()
  var birthDate = NSDate?

  init(_ map: [String : Any]) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.relation("spouse")
    parents   <- map.relations("parents")
    birthDate <- map.transform("birth_date", transformer: { (value: String) -> NSDate? in
      let dateFormatter = NSDateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd"
      return dateFormatter.dateFromString(value)
    })
  }
}

let dictionary = [
  "first_name" : "Taylor",
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin",
              "last_name" : "Harris"],
  "parents" : [
             ["first_name" : "Andrea",
              "last_name" : "Swift"],
              ["first_name" : "Scott",
              "last_name" : "Swift"]
  ],
  "birth_date": "1989-12-13"
]
let model = Person(dictionary)

KeyPath

Tailor supports mapping values using deep keyPath

struct Book: Mappable {

  var title: String = ""
  var publisherName: String = ""
  var authorName: String = ""
  var firstReviewerName: String = ""

  init(_ map: [String : Any]) {
    title <- map.resolve(keyPath: "title")
    publisherName <- map.resolve(keyPath: "publisher.name")
    authorName <- map.resolve(keyPath: "info.author.name")
    firstReviewerName <- map.resolve(keyPath: "meta.reviewers.0.info.name.first_name")
  }
}

Resolving value types.

Tailor supports mapping values from dictionaries using type specific functions.

dictionary.boolean("key")
dictionary.double("key")
dictionary.float("key")
dictionary.int("key")
dictionary.string("key")

You can also use value(forKey:ofType:), it works like this.

dictionary.value(forKey: "key", ofType: Bool.self)
dictionary.value(forKey: "key", ofType: Double.self)
dictionary.value(forKey: "key", ofType: Float.self)
dictionary.value(forKey: "key", ofType: Int.self)
dictionary.value(forKey: "key", ofType: String.self)

All of these methods returns an optional value.

Installation

Tailor is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Tailor'

Contribute

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create pull request

Who made this?

More Repositories

1

Hue

🎨 Hue is the all-in-one coloring utility that you'll ever need.
Swift
3,463
star
2

Gray

πŸŒ“ Tailor your macOS Mojave experience
Swift
1,325
star
3

Spots

🎍 Spots is a cross-platform view controller framework for building component-based UIs
Swift
1,314
star
4

Blueprints

πŸŒ€ Blueprints - A framework that is meant to make your life easier when working with collection view flow layouts.
Swift
990
star
5

KeyboardCowboy

⌨️ The missing keyboard shortcut utility for macOS
Swift
740
star
6

Syncalicious

🍫 Syncalicious
Swift
356
star
7

Vaccine

πŸ’‰ Vaccine - Make your apps immune to recompile-disease
Swift
303
star
8

Family

🚸 A child view controller framework that makes setting up your parent controllers as easy as pie.
Swift
250
star
9

Versions

❇️Helping you find inner peace when comparing version numbers in Swift.
Swift
208
star
10

MarvinXcode

πŸ”¨A collection of nifty commands for your everyday workflow in Xcode
Swift
125
star
11

Differific

β›½ Differific - a fast and convenient diffing framework.
Swift
124
star
12

Coda-2-Modes

Modes for Coda 2
AppleScript
61
star
13

UserInterface

πŸš₯ UserInterface - a collection of convenience extensions specifically tailored to building user interfaces in Swift.
Swift
51
star
14

ToTheTop

πŸ”To the top - A small macOS application to help you scroll to the top.
Swift
36
star
15

Zcode

Work around Apples restriction with running Xcode 6.4 on El Capitan Developer Preview 2
Swift
25
star
16

NSString-ZENInflections

Returns camelCased, UpperCamelCased, dashed-case, snake_cased representations of an NSString
Objective-C
25
star
17

Inflection

The Optimus Prime of string inflection
Swift
22
star
18

MouseDef

🐭Move and resize windows by holding down modifier keys
Swift
21
star
19

ChangeMarks

Change Marks helps you to keep track of your most recent changes by giving them a different background color.
Objective-C
14
star
20

OSX-Configuration

My personal OS X configurations
Shell
10
star
21

ZenCode

πŸ”¨ZenCode for Xcode - A collection of nifty commands for your everyday workflow in Xcode.
Swift
9
star
22

Apps

Swift
7
star
23

Voodoo

πŸ’€ Voodoo is a set of Sourcery templates to make you do more with less.
Swift
7
star
24

Houston

Swift
6
star
25

WindowFlex

Helps you flex your Xcode window muscles
Objective-C
6
star
26

Storage

Swift
5
star
27

AXEssibility

Swift
5
star
28

Mapper

An object mapper for Swift
Swift
4
star
29

Marvin-Xcode-Extension

πŸ”¨A collection of nifty commands for your everyday workflow in Xcode 8
Swift
4
star
30

Xcode-Templates

πŸ”¨ Xcode templates
Swift
4
star
31

Goldfish

Something something secret ... goldfish.
Swift
3
star
32

TextMate-1-Bundle

Commands and Snippets I use everyday at work
3
star
33

coda-command-line

Coda 2 command line tool
Objective-C
3
star
34

XcodeConfiguration

3
star
35

C.mode

C mode for Coda 2
AppleScript
3
star
36

DirectoryObserver

Observing the file-system, easy as 1 2 3!
Swift
3
star
37

GoldenRetriever

Swift
2
star
38

dotfiles

My dotfiles
Vim Script
2
star
39

xcode-fish

Find and open xcworkspace or xcproject in Xcode.
Shell
2
star
40

LaunchArguments

Swift
2
star
41

xcode-espresso-tribute-theme

A light theme inspired by the Espresso - The Web Editor
2
star
42

xcode-ios-project

Ruby
2
star
43

MachPort

Swift
2
star
44

ZENFramework

Just another PHP MVC framework
PHP
2
star
45

HideDockAndStageManager

Swift
2
star
46

Windows

Swift
2
star
47

NSString-ZENVersions

Helping you find inner peace when comparing version numbers.
Objective-C
2
star
48

ApacheConf.mode

Apache configuration mode for Coda 2
1
star
49

theme-oh-my-sushi

oh-my-sushi
Shell
1
star
50

SidebarFlex

Objective-C
1
star
51

Retina

Swift
1
star
52

KeyCodes

Swift
1
star
53

StageManagerAppWindowGroupingBehavior

Swift
1
star
54

Swift.mode

1
star
55

Ruby.mode

Ruby mode for Coda 2
AppleScript
1
star
56

DockManager

Swift
1
star
57

xc

Swift
1
star
58

haml.mode

HAML mode for Coda 2
1
star
59

ERB.mode

Ruby ERB mode for Coda 2
AppleScript
1
star
60

emolator

TBD
1
star
61

DefaultKeyBinding

Custom keyboard shortcuts for OS X
1
star