I am doing the exercism.io programming exercises and the tests I have to perform on my code has the goal to compare to dicts with each other. The sourcecode of the exercise can be found here https://github.com/exercism/xswift/tree/master/word-count
我正在进行exercism.io编程练习,我必须对我的代码执行的测试的目标是与dicts相互比较。练习的源代码可以在这里找到https://github.com/exercism/xswift/tree/master/word-count
As of what I have understood bridgeToObjectiveC is apples internal methods for doing things and therefore they have been removed. With them i get '[S : T]' does not have a member named 'bridgeToObjectiveC'
which is very understandable if they have removed it.
据我所知,bridgeToObjectiveC是苹果内部处理方法,因此它们已被删除。有了它们,我得到'[S:T]'没有一个名为'bridgeToObjectiveC'的成员,如果他们删除了它,这是非常容易理解的。
Without the method using only the params in the AssertEquals call i get '[S : T]' does not conform to protocol 'Equatable'
. Is two dicts not comparable in Swift? How would I do to get them comparable?
如果没有在AssertEquals调用中仅使用params的方法,我得到'[S:T]'不符合协议'Equatable'。两个词在Swift中无法比较吗?我怎么做才能让它们具有可比性?
3 个解决方案
#1
7
You can check equality of dictionaries as long as the values are Equatable
. Modify XCTAssertEqualDictionaries
to include a generic constraint:
只要值为Equatable,就可以检查字典的相等性。修改XCTAssertEqualDictionaries以包含通用约束:
func XCTAssertEqualDictionaries<S, T: Equatable>(first: [S:T], _ second: [S:T]) {
XCTAssert(first == second)
}
#2
3
Try
尝试
XCTAssertEqual(swiftDict as NSObject, objCDict as NSObject)
Forces the compiler to just chill out and call the isEqual:
method on both.
强制编译器只是放松并在两者上调用isEqual:方法。
#3
1
No, Swift dictionaries are not directly comparable. For the purposes of unit-testing, you can either do manual comparisons of their sizes and pair-wise element comparisons, or you can do the easy thing and create NSDictionary
s out of them and compare them that way.
不,Swift词典不能直接比较。出于单元测试的目的,您可以手动比较它们的大小和成对元素比较,或者您可以轻松完成并从中创建NSDictionarys并以这种方式进行比较。
#1
7
You can check equality of dictionaries as long as the values are Equatable
. Modify XCTAssertEqualDictionaries
to include a generic constraint:
只要值为Equatable,就可以检查字典的相等性。修改XCTAssertEqualDictionaries以包含通用约束:
func XCTAssertEqualDictionaries<S, T: Equatable>(first: [S:T], _ second: [S:T]) {
XCTAssert(first == second)
}
#2
3
Try
尝试
XCTAssertEqual(swiftDict as NSObject, objCDict as NSObject)
Forces the compiler to just chill out and call the isEqual:
method on both.
强制编译器只是放松并在两者上调用isEqual:方法。
#3
1
No, Swift dictionaries are not directly comparable. For the purposes of unit-testing, you can either do manual comparisons of their sizes and pair-wise element comparisons, or you can do the easy thing and create NSDictionary
s out of them and compare them that way.
不,Swift词典不能直接比较。出于单元测试的目的,您可以手动比较它们的大小和成对元素比较,或者您可以轻松完成并从中创建NSDictionarys并以这种方式进行比较。