This simple project just makes the Swift compiler crash in init(comparator: KeyPathComparator<RemoteComputer>)
:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
enum CSItemOrderType: Int, Codable, CaseIterable, CustomStringConvertible {
case readableName
case lastConnectionDate
case numberOfConnections
var description: String {
switch self {
case .readableName: return "STR_NAME"
case .lastConnectionDate: return "STR_LAST_CONN_DATE"
case .numberOfConnections: return "STR_ORDER_MOST_CONNECTED"
}
}
}
struct CSItemOrder: Codable {
static let allCases = [CSItemOrder(type: .readableName),
CSItemOrder(type: .lastConnectionDate, order: .reverse),
CSItemOrder(type: .numberOfConnections, order: .reverse)]
static let allSortOrders = [KeyPathComparator(\RemoteComputer.readableName),
KeyPathComparator(\RemoteComputer.lastConnectionDate),
KeyPathComparator(\RemoteComputer.numberOfConnections)]
let type: CSItemOrderType
var order: SortOrder
var comparator: KeyPathComparator<RemoteComputer> {
switch type {
case .readableName: return KeyPathComparator(\RemoteComputer.readableName, order: order)
case .lastConnectionDate: return KeyPathComparator(\RemoteComputer.lastConnectionDate, order: order)
case .numberOfConnections: return KeyPathComparator(\RemoteComputer.numberOfConnections, order: order)
}
}
init(type: CSItemOrderType, order: SortOrder = .forward) {
self.type = type
self.order = order
}
init(comparator: KeyPathComparator<RemoteComputer>) throws {
switch comparator.keyPath {
case \RemoteComputer.readableName: self.init(type: .readableName, order: comparator.order)
case \RemoteComputer.lastConnectionDate: self.init(type: .lastConnectionDate, order: comparator.order)
case \RemoteComputer.numberOfConnections: self.init(type: .numberOfConnections, order: comparator.order)
default:
print("Unsupported keyPath: \(comparator.keyPath)")
throw ItemOrderError.unsupportedKeyPath
}
}
}
struct RemoteComputer: Codable, Hashable, Identifiable {
let id: Int
let name: String
let hostname: String?
let localIpAddress: String?
let vncPort: Int
let sshPort: Int
let publicIpAddress: String?
let publicPort: Int
let macAddress: String
let scVersion: String
let manualMode: Int
let uuid: String
let lastMappingStatus: String?
let isAwake: Bool
let unregistered: Bool
let osVersion: String?
let lastUpdate: Date
let remoteAddress: String?
let lastKeyboardLocale: String?
let macSystemShortcuts: [String: [Int32]]?
var readableName: String {
return name.removingPercentEncoding ?? name
}
var lastConnectionDate: Date {
return Date.now
}
var unwrappedPublicIpAddress: String {
return publicIpAddress ?? NSLocalizedString("STR_NOT_AVAILABLE", comment: "")
}
var numberOfConnections: Int {
return 0
}
}
enum ItemOrderError: Error {
case unsupportedKeyPath
}
This code works fine in previous betas or Xcode 15.