- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在Swift中,有没有类似map()
的Collection函数,不是把集合变成数组,而是变成字典?
我想写的一个例子:
let collection = [1, 2, 3, 4, 5] // Could also be a dictionary itself
let dictionary: [Int : String] = collection.map{
$0 : "Number: \($0)"
}
我想没有这样的功能,类似但优雅的东西会是什么样子?
最佳答案
下面是如何完成的草图。它使用从元素闭包返回的元组,而不是您的 f1($0) : f2($0)
伪代码。请注意,元组元素可以是任何类型,只要第一个(将用作键)是可哈希的。
let collection = [1, 2, 3, 4, 5]
/* OP's requested form:
let dictionary: [Int : String] = collection.map{
$0 : "Number: \($0)"
}
*/
// Return a tuple of (key, value) from the closure...
extension CollectionType {
func mapd<Tk: Hashable, Tv>(elementClosure: (Self.Generator.Element) -> (Tk, Tv) ) -> [Tk : Tv] {
var returnDict = [Tk : Tv]()
for i in self {
let (k, v) = elementClosure(i)
returnDict[k] = v
}
return returnDict
}
}
let dictionary: [Int : String] = collection.mapd{ ($0, "Number: \($0)") }
print("\(dictionary)") // "[5: "Number: 5", 2: "Number: 2", 3: "Number: 3", 1: "Number:1]"
// Neither of the dictionary's elements need be the type of the collection,
// it's entirely up to what the closure returns.
let dictionary2: [Double : String] = collection.mapd{ (Double($0), "Number: \($0)") }
print("\(dictionary)") // "[5: "Number: 5", 2: "Number: 2", 3: "Number: 3", 1: "Number:1]"
关于swift - 是否有将集合转换为字典的 CollectionType 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33171129/
我有一个包含 collectionType 的 formBuilder。我希望能够对电子邮件字段施加限制,以确保当用户验证时,表单中不会多次出现相同的电子邮件地址 我已经: RegistrationC
protocol Decodable { init?(data: [String: AnyObject]) } struct A: Decodable { var data: [Str
我正在编写一个递归下降解析器。我希望我的解析器能够处理 UInt8 的任何(或至少“许多”)集合(例如,不仅是 Swift.Array) func unpack(t: T) { let m =
我正在定义一个打印出 Any 实例的函数.如果它是 NSArray或 CollectionType它打印出它有多少个项目,最多 10 个项目: static func prettyPrint(any:
我正在为一个 Symfony 项目苦苦挣扎,由于我对该框架没有那么丰富的经验,所以我无法弄清楚我是否有设计缺陷,Symfony 是否无法处理我的用例,或者我是否只需要找到正确的方法。 这里是 : 我有
这是我的示例代码 import Foundation // ar4random_uniform extension CollectionType where Self.Index.Distance =
我正在尝试为包含特定类型 FooType 的元素的集合类型创建扩展,这工作得很好: extension CollectionType where Generator.Element == FooCla
例如,假设我有一个结构体 AdjacencyList,我想在其中指定存储顶点的容器类型,以便用户可以选择 Set(如果他们)不想要重复项,或者 Array 如果需要重复项。 (我省略了类型的协议(pr
我创建了这样的枚举: enum AudioCodecsType: String { case MPEG4AAC = "MPEG-4 Audio", iLBC = "iLBC" stat
我正在尝试重载 -= 运算符以获得与 CollectionType 和 SequenceType 上的 += 重载相同(但相反)的行为。但是,我在使用以下代码时遇到了麻烦: func -=(inout
仅当表单中存在来自 CollectionType 的输入时,如何验证 AdMenuLinkType 表单中的 CollectionType。 例如:当我在浏览器中打开添加表单时,我可以创建没有路由参数
问题: 考虑以下订单表单有很多要求: Title: [_________________] REQUIREMENTS: What sizes? [X] Small [X] Me
我正在尝试实现一个符合/扩展 CollectionType 的协议(protocol),但是它不采用显然是元素类型的单一泛型类型,所以我想能够计算/强制 Generator.Element 的类型。
只有当对象包含在另一个数组中时,我才想删除数组中的对象。 myArray = myArray - otherArray 如何将此行为添加为 CollectionType 的扩展 最佳答案 因此,您可以
在Swift中,有没有类似map()的Collection函数,不是把集合变成数组,而是变成字典? 我想写的一个例子: let collection = [1, 2, 3, 4, 5] // Coul
首先,我知道这是完全错误的语法,但它说明了我想做什么: public func x(completion: CollectionType -> Void) { } 基本上,我想做的是编
在我的模型中,我有一个 Recipe 实体和 Ingredient 实体。在 Recipe 实体中,关系定义如下: /** * @ORM\OneToMany(targetEntity="Ingred
这是我关于 stackoverflow 的第一个问题;直到现在,我只是寻找我的问题的答案并找到了它们。但现在似乎我有一个在这里没有人或至少没有人偶然发现的。 关于我的问题: 在我的类(class) F
var myQuery = from product in _repository.Query() join prodLocalization in _re
我在 Symfony2 中使用带有集合类型的 select2 时遇到问题 当我使用基本选择时,没问题,我有自己的选择,但是当我使用 select2 时,“找不到结果”。此问题特别针对集合类型(text
我是一名优秀的程序员,十分优秀!