s-hunter
s-hunter

Reputation: 25806

What is the meaning of left angle bracket hyphen (<-) in Swift?

I am new to Swift and first time seeing the use of back arrow followed with a hyphen in Swift. I can guess this is assigning a value in the following code, but why can't it just use the equal sign =? When do we want to use this left angle bracket <- instead of = sign? Any other use case for <-? Here is the code which uses this <- from https://github.com/tristanhimmelman/ObjectMapper . The library states ObjectMapper uses the <- operator to define how each member variable maps to and from JSON., does it mean this operator is invented by this library and only applicable in this library?

struct Temperature: Mappable {
    var celsius: Double?
    var fahrenheit: Double?

    init?(map: Map) {

    }

    mutating func mapping(map: Map) {
        celsius     <- map["celsius"]
        fahrenheit  <- map["fahrenheit"]
    }
}

Upvotes: 0

Views: 204

Answers (1)

Bob Peterson
Bob Peterson

Reputation: 696

Yes, it is defined in ObjectMapper, in IntegerOperators.swift, and perhaps other places I haven't checked.

https://github.com/tristanhimmelman/ObjectMapper/blob/master/Sources/IntegerOperators.swift

There are several operator definitions in that module for various argument type combinations. Example:

public func <- <T: UnsignedInteger>(left: inout T, right: Map) {
    switch right.mappingType {
    case .fromJSON where right.isKeyPresent:
        let value: T = toUnsignedInteger(right.currentValue) ?? 0
        FromJSON.basicType(&left, object: value)
    case .toJSON:
        left >>> right
    default: ()
    }
}

Upvotes: 1

Related Questions