使用 xcode 16 后,我收到以下代码的构建错误
Cast from 'ObservableCollection<E>.SectionsChange' (aka 'CollectionChange<Int, ObservableArray<E>>') to unrelated type 'ObservableCollection<Int>.SectionsChange' (aka 'CollectionChange<Int, ObservableArray<Int>>') always fails
对于我实现的部分协议方法,第一次转换仍然有效
class ObservableCollectionDelegateMock: ObservableCollectionDelegate {
var collectionDidRowBatchUpdateAtExpect = LIExpect()
var collectionDidSectionBatchUpdateAtExpect = LIExpect()
var rowBatchUpdates: ObservableCollection<Int>.RowsChange?
var sectionBatchUpdates: ObservableCollection<Int>.SectionsChange?
func collection<E>(_ collection: ObservableCollection<E>, rowsDidChange change: ObservableCollection<E>.RowsChange) {
collectionDidRowBatchUpdateAtExpect.called(actualParams: nil)
rowBatchUpdates = change as? ObservableCollection<Int>.RowsChange
}
func collection<E>(_ collection: ObservableCollection<E>, sectionsDidChange change: ObservableCollection<E>.SectionsChange) {
collectionDidSectionBatchUpdateAtExpect.called(actualParams: nil)
sectionBatchUpdates = change as? ObservableCollection<Int>.SectionsChange
}
}
这是
ObservableCollectionDelegate
的定义
这是协议 delcare 这两个方法,要重写的子类
public protocol ObservableCollectionDelegate: AnyObject {
func collection<E>(_ collection: ObservableCollection<E>, rowsDidChange change: ObservableCollection<E>.RowsChange)
func collection<E>(_ collection: ObservableCollection<E>, sectionsDidChange change: ObservableCollection<E>.SectionsChange)
}
这是这两个别名的定义。
class ObservableCollection<Element> {
// MARK: - Nested Types
public typealias RowsChange = CollectionChange<IndexPath, Element>
public typealias SectionsChange = CollectionChange<Int, ObservableArray<Element>>
.......
}
这是 ObservableArray 的定义
public class ObservableArray<T> {
// MARK: - Public Properties
public var count: Int {
return elements.count
}
public var isEmpty: Bool {
return elements.isEmpty
}
public var hasElements: Bool {
return !elements.isEmpty
}
/// Returns the snapshot of current state as a normal array. (Essentially returning the copy of itself).
public var snapshot: [T] {
return elements
}
// MARK: - Internal Properties
internal(set) public var elements: [T]
// MARK: - Lifecycle
public init<S>(elements: S) where S: Sequence, S.Element == T {
self.elements = [T](elements)
}
......
据我了解,似乎对于 xcode 16,如果存在嵌套泛型类型,则无法通过编译器区分泛型类型。
我们的案例是
ObservableCollection.SectionsChange'(又名'CollectionChange
想知道是否有办法解决这个问题?或者这是 swift 6 禁止做的事情?
谢谢
一种不同的方法,因为您在强制转换时期望泛型类型为 Int,所以所有泛型类型 E 都应该是 Int 或相同类型。 因此,您可以更改 ObservableCollectionDelegateMock 以拥有通用元素
class ObservableCollectionDelegateMock<E> {
var rowBatchUpdates: ObservableCollection<E>.RowsChange?
var sectionBatchUpdates: ObservableCollection<E>.SectionsChange?
func collection(_ collection: ObservableCollection<E>, rowsDidChange change: ObservableCollection<E>.RowsChange) {
rowBatchUpdates = change
}
func collection(_ collection: ObservableCollection<E>, sectionsDidChange change: ObservableCollection<E>.SectionsChange) {
sectionBatchUpdates = change
}
}