gpt4 book ai didi

ios - 在 Swift 中过滤类数组

转载 作者:行者123 更新时间:2023-11-28 08:48:00 24 4
gpt4 key购买 nike

我正在尝试过滤包含类的数组,以便只有在另一个数组中找到的类才会添加到数组中。这是我目前所拥有的:

class Match : Equatable {
var name: String
var value: String

init(name: String, value: String) {
self.name = name
self.value = value
}

func ==(lhs: Match, rhs: Match) -> Bool {
return lhs.name == rhs.name && lhs.value == rhs.value
}

// attempt to filter array containing Match structs

let terms = [Match]()
let someOtherObjects = [Match]()
let sampleMatch = Match(name: "someName", value: "someValue")
someOtherObjects.append(sampleMatch)

filteredTerms = terms.filter { term in
if attemptedCombos.contains(sampleMatch) {
return true
}
}

但是编译器不允许我构建错误:

"Cannot convert value of type 'Match' to expected argument type '@noescape (Match) throws -> Bool'

有什么想法吗?

最佳答案

使用 Set 更新(因为看起来您想要两个 [Match] 数组的交集)。除了 Equatable 之外,您还必须让您的 Match 类符合 Hashable 以允许其实例作为 Set 中的元素

class Match : Equatable, Hashable {
var name: String
var value: String

init(_ name: String, _ value: String) {
self.name = name
self.value = value
}

var hashValue: Int {
get {
return name.hashValue << 20 + value.hashValue
}
}
}

func ==(lhs: Match, rhs: Match) -> Bool {
return lhs.name == rhs.name && lhs.value == rhs.value
}

例子:

/* Example */
var mySetA : Set<Match> = [Match("foo", "bar"), Match("foo", "foo"), Match("barbar", "foo")]

var mySetB = Set<Match>()
mySetB.insert(Match("barbar", "bar"))
mySetB.insert(Match("bar", "foo"))
mySetB.insert(Match("foo", "bar"))
mySetB.insert(Match("foo", "foo"))

let myIntersect = mySetA.intersect(mySetB)
for match in myIntersect {
print("name: " + match.name + ", value: " + match.value)
}
/* name: foo, value: foo
name: foo, value: bar */

在与 OP 交谈后,我们已经解决了聊天中的问题。我不确定这里的约定是什么,但我将总结 OP 在聊天中提供的附加信息以及问题的解决方案。将上面的 block 视为上述问题的解决方案,将下面的 block 视为对上述问题的非常狭窄的解决方案,并补充了 OP 的更多细节。


  • “过滤器”对象数组与要过滤的数组 (Match) 属于不同的类类型 (Tern),这两个类共享某个类属性。
  • 对 OP 的一个自然询问是让这两个类拥有一个共同的父类(super class)是否可以接受;是的。
  • 除上述之外,这两个类的共同属性之一是自定义枚举类型,由作者在聊天中发布。

最终使用的解决方案,如上Set.intersect():

/* custom enum given by OP in chat */
enum Declension : String {
case firstDeclensionFem = "a:a:am:ae:ae:a:ae:ae:as:arum:is:is"
case secondDeclensionMasc = "us:er:um:i:o:o:i:i:os:orum:is:is"
case secondDeclensionNeu = "um:um:um:i:o:o:a:a:a:orum:is:is"
case thirdDeclensionMasc = " : :em:is:i:e:es:es:es:um:ibus:ibus"
case thirdDeclensionMascSpecial = " : :em:is:i:e:es:es:es:ium:ibus:ibus"
case fourthFem = "us:us:um:us:ui:u:us:us:us:uum:ibus:ibus"
case fourthNeu = "u:u:u:us:u:u:ua:ua:ua:uum:ibus:ibus"
case fifthMasc = "es:es:em:ei:ei:e:es:es:es:erum:ebus:ebus"
case unknown

static let allValues = [firstDeclensionFem, secondDeclensionMasc, secondDeclensionNeu, thirdDeclensionMasc, thirdDeclensionMascSpecial, fourthFem, fourthNeu, fifthMasc]
}

/* use a superclass and let the sets below have members that
are declared to be of this superclass type */
class MyMatchTypes : Equatable, Hashable {
var latin: String
var declension: Declension

init(_ latin: String, _ declension: Declension) {
self.latin = latin
self.declension = declension
}

var hashValue: Int {
get {
return latin.hashValue << 20 + declension.hashValue
}
}
}

func ==(lhs: MyMatchTypes, rhs: MyMatchTypes) -> Bool {
return lhs.latin == rhs.latin && lhs.declension == rhs.declension
}

/* the two classes mentioned in chat: use as subclasses */
class Term : MyMatchTypes {
var meaning: String
var notes: String
var genStem: String

init(_ latin: String, _ declension: Declension, _ meaning: String, _ genStem: String, _ notes: String) {

self.meaning = meaning
self.notes = notes
self.genStem = genStem

super.init(latin, declension)

}
}

class Match : MyMatchTypes {

// ... add stuff

// super init is OK
}

/* Example */
/* ----------------------------------------------- */
/* Set of `Match` objects */
var mySetA = Set<MyMatchTypes>()
mySetA.insert(Match("foo", Declension.firstDeclensionFem))
mySetA.insert(Match("bar", Declension.fourthFem))
mySetA.insert(Match("foofoo", Declension.fourthFem))
mySetA.insert(Match("barbar", Declension.fifthMasc))

/* Set of `Term` objects */
var mySetB = Set<MyMatchTypes>()
mySetB.insert(Term("fooshy", Declension.fourthFem, "a", "b", "c"))
mySetB.insert(Term("barbar", Declension.fifthMasc, "a", "b", "c"))
mySetB.insert(Term("bar", Declension.fourthFem, "a", "b", "c"))
mySetB.insert(Term("foofoo", Declension.firstDeclensionFem, "a", "b", "c"))
mySetB.insert(Term("foobar", Declension.fourthFem, "a", "b", "c"))

/* compute intersection */
let myIntersect = mySetA.intersect(mySetB)
for obj in myIntersect {
print("latin: " + obj.latin + ", declension: \(obj.declension)")
}
/* latin: barbar, declension: fifthMasc
latin: bar, declension: fourthFem */

关于ios - 在 Swift 中过滤类数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com