3

I was looking at ObjectMapper libary. And noticed the <- operator.

How does this exactly work ?

// Mappable
func mapping(map: Map) {
    username    <- map["username"]
    age         <- map["age"]
    weight      <- map["weight"]
    array       <- map["arr"]
    dictionary  <- map["dict"]
    bestFriend  <- map["best_friend"]
    friends     <- map["friends"]
    birthday    <- (map["birthday"], DateTransform())
}

Also how does the following line work

birthday    <- (map["birthday"], DateTransform())

I understand that birthday is now a tuple. Which can be access by

self.birthday.0
self.birthday.1

Although the property is defined like so

var birthday: NSDate?

How does a tuple respond as a NSDate (in this case) ?

Thanks!

Prakash Raman
  • 13,319
  • 27
  • 82
  • 132

1 Answers1

6

If you look at his Operators.Swift file in his core library you'll find all of the overloads for his custom operator.

He defines his custom operator:

infix operator <- {}

Then he has quite a few overloads for his operator, here's the first overload from his list:

/// Object of Basic type
public func <- <T>(inout left: T, right: Map) {
    switch right.mappingType {
    case .FromJSON:
        FromJSON.basicType(&left, object: right.value())
    case .ToJSON:
        ToJSON.basicType(left, map: right)
    }
}

You'll need to go through each of his overloads to understand how they function, and he has dozens of them.

Operators

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135