I’ve simply up to date to Xcode 16 and my venture is plagued by warnings as a result of change to mark AnyHashable
as not-sendable. I used to be counting on AnyHashable all through my app, making a ViewModel layer that returned a datasource of kind UITableViewDiffableDataSource
. Now i am attempting to construct one thing that’s Hashable and Sendable, and will be slotted in with out a lot refactoring. Going off different examples on-line I created this new struct modeled after AnyHashable
public struct AnyHashableSendable: Hashable, Sendable {
public let base: any Hashable & Sendable
public init(_ base: H) the place H : Hashable {
self.base = base
}
public static func == (lhs: Self, rhs: Self) -> Bool {
AnyHashable(lhs.base) == AnyHashable(rhs.base)
}
public func hash(into hasher: inout Hasher) {
base.hash(into: &hasher)
}
}
However this present implementation comes with a couple of annoyances that i am seeking to resolve in a manner that matches the apple implementation
-
If I’ve a variable
foo
of kindAnyHashable
and I need to solid it, I can merely dolet newBar = foo as? bar
. However with a variable of kindAnyHashableSendable
I now should name the propertybase
to attain the identical, like solet newBar = foo.base as? bar
. How does the inbuilt AnyHashable keep away from the necessity to name itsbase
property? -
If I outline an array or 2nd array of AnyHashable (
[AnyHashable]
,[[AnyHashable]]
) I can append any struct that conforms to hashable and it’ll robotically solid it. E.g.
var array: [AnyHashable] = []
array.append(FooBar(prop1: "check", prop2: "check"))
However with AnyHashableSendable, every merchandise have to be initialised as the brand new object kind, e.g.
var array: [AnyHashableSendable] = []
array.append(.init(FooBar(prop1: "check", prop2: "check")))
I’ve tried to unravel a minimum of the second by creating my very own customized assortment kind, with overridden features that settle for AnyHashable and easily name the init and retailer it. Whereas it really works, the variations require a good little bit of refactoring and annoying customized code/sorts as a substitute of the present logic have been used too. Is there a manner i am unaware of to unravel these two points in order that my new kind features like the present AnyHashable?