Reputation: 124
I want to use CaseIterable on FileManager.SearchPathDirectory:
extension FileManager.SearchPathDirectory: CaseIterable {}
but I get the error: "Type 'FileManager.SearchPathDirectory' does not conform to protocol 'CaseIterable'".
I suspect that RawValue is causing the problem. Next step I tried to write an enum with RawValue:
struct My {
enum Number: UInt {
case One = 1
case Two = 2
}
}
extension My.Number: CaseIterable {}
for item in My.Number.allCases {
print(item)
}
Seems to be no problem. In the second attempt, I added the protocol stubs:
extension FileManager.SearchPathDirectory: CaseIterable {
public typealias AllCases = UInt
public static var allCases: [Self] = [.adminApplicationDirectory, .allApplicationsDirectory, .desktopDirectory]
}
The problem remains. Does anyone know the solution?
Upvotes: 1
Views: 299
Reputation: 236538
CaseIterable
protocol only requires you to implement a static var allCases
with a collection of all values of this type and an a associatedtype AllCases
which is a type that can represent a collection of all values of this type.
The issue in your implementation is that your AllCases
typealias should be a collection of FileManager.SearchPathDirectory
which can be also written as [Self]
instead of UInt
:
extension FileManager.SearchPathDirectory: CaseIterable {
public typealias AllCases = [Self]
public static var allCases: AllCases = [.adminApplicationDirectory, .allApplicationsDirectory, .desktopDirectory]
}
for directory in FileManager.SearchPathDirectory.allCases {
print(directory.rawValue)
}
This will print:
4
100
12
Upvotes: 1