map - 您可以使用 map 创建没有包装器的实例吗?

标签 map swift

在 Python 中,您可以将类的名称作为参数传递给 map 以创建该类的实例:

class Point(object):
    def __init__(self, (x, y)):
        self.x = x
        self.y = y

coords = [(1., 2.), (3., 4.)]
pts = map(Point, coords)

这通常被证明是一种方便的模式,所以我想在 Swift 中尝试同样的事情。


首先,我们设置我们的 Point 类:

import Cocoa

class Point
{
    var x: Float
    var y: Float

    init(x: Float, y: Float) {
        self.x = x
        self.y = y
    }
}

var pt = Point(x: 1, y: 2) // works fine

但是当我们尝试使用 .map 创建实例时,我们得到一个错误:

let coords: (Float,Float)[] = [(1, 2), (3, 4)]

// (Point).Type is not convertible to '(Float, Float) -> $T3'
var pts = coords.map(Point)
// Initializer cannot be referenced without arguments
var pts = coords.map(Point.init)

对我来说有趣的是,如果我们首先定义一个包装函数,这确实有效:

func wrapper(x: Float, y: Float) -> Point {
    return Point(x: x, y: y)
}

// This *is* successful
var ptsWrapped = coords.map(wrapper)

好的,现在我很好奇这是否禁止在方法上使用 map:

extension Point {
    func newPointByAdding(x: Float, y: Float) -> Point {
        return Point(x: self.x + x, y: self.y + y)
    }
}

// This works as expected
var origin = Point(x: 0, y: 0)
var ptsAdded = coords.map(origin.newPointByAdding)

...不,这很好用。


我承认我还没有花太多时间在 swift 上,所以我可能在规范中遗漏了一些禁止这样做的东西。

是否可以使用 map 在 swift 中创建类/结构的新实例?

如果不是,为什么不呢?

  • 是因为 init 不是 func 吗?
  • 是否与命名参数在某些上下文中不能转换为位置参数有关?

最佳答案

Swift 2 更新:

提交错误有效!

在 Swift 2 中,这现在可以通过 coords.map(Point.init) 来实现。 :


旧答案:

  • is it because init is not a func?

是的。在 Swift 中,一个 function type “由用箭头分隔的参数和返回类型组成(->)”,和map定义为 func map<U>(transform: (T) -> U) -> [U] ,即它接受一个函数。在 the grammar ,“函数声明”和“初始化器声明”是分开处理的。后者没有返回类型,因为它不是真正的函数,只是用于初始化实例的代码块。如果你试图通过 Point.init ,您将收到错误“无法在没有参数的情况下引用初始化程序”。

File a bug!

关于map - 您可以使用 map 创建没有包装器的实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24718134/

相关文章:

c++ - std::map - 删除最后一个元素

map - 获取结构元素的静态类型

python - 如何在 python 中通过比较映射 2 个列表

ios - 如何使 NSMutableAttributedString 响应设置应用程序中的动态类型文本

swift - 从一个文本字段转到另一个文本字段

c++ - 有什么hashmap可以做而map不能做的事吗?

C++:std::map 排序

ios - 当应用程序处于后台时,在内存中保留一个 Firebase 监听器

swift - Homekit触发器,如何调用

swift - RxSwift - 链接 Observables 和 Singles