在SwiftUI中对带有本地化字符串的数组进行排序,可以按照以下步骤进行:
localizedStandardCompare
方法对数组进行排序。这个方法会根据当前用户的语言环境进行比较,确保正确的排序顺序。示例代码如下:let localizedArray = ["Hello", "Bonjour", "Hola"]
let sortedArray = localizedArray.sorted { $0.localizedStandardCompare($1) == .orderedAscending }
struct ContentView: View {
let localizedArray = ["Hello", "Bonjour", "Hola"]
let sortedArray = localizedArray.sorted { $0.localizedStandardCompare($1) == .orderedAscending }
var body: some View {
List(sortedArray, id: \.self) { item in
Text(item)
}
}
}
领取专属 10元无门槛券
手把手带您无忧上云