The problem here is that there are two map(_:)
functions. One for sequences:
public protocol Sequence {
// ...
/// Returns an array containing the results of mapping the given closure
/// over the sequence's elements.
///
/// - Parameter transform: A mapping closure. `transform` accepts an
/// element of this sequence as its parameter and returns a transformed
/// value of the same or of a different type.
/// - Returns: An array containing the transformed elements of this
/// sequence.
func map<T>(_ transform: (Iterator.Element) throws -> T) rethrows -> [T]
// ...
}
and one for optionals:
public enum Optional<Wrapped> : ExpressibleByNilLiteral {
// ...
/// Evaluates the given closure when this `Optional` instance is not `nil`,
/// passing the unwrapped value as a parameter.
///
/// - Parameter transform: A closure that takes the unwrapped value
/// of the instance.
/// - Returns: The result of the given closure. If this instance is `nil`,
/// returns `nil`.
public func map<U>(_ transform: (Wrapped) throws -> U) rethrows -> U?
// ...
}
Therefore when you call map
on a [SomeClass]?
, the second map
function will be used, where the transformation function parameter will be of type [SomeClass]
, as map
will unwrap it for you, and apply a given transformation to it.
However, when you call map
on a [SomeClass]
, the first map
function will be used, where the elements will be iterated through – applying the transformation function to each of them. Therefore the parameter type of the transformation function will be SomeClass
.
One amusing solution therefore would be to use map
twice – once to unwrap, once to apply a transform to the elements:
self.wrappedObjects = objects.map{ $0.map{ WrapperClass($0) } }
However, this is absolutely ridiculous, you should definitely use optional chaining as Rob suggests.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…