如何在 SwiftUI 中使用本地化字符串对数组进行排序
How to sort a array with localized String in SwiftUI
您好,我的问题是我想按 object 的标题 属性 对 object 的数组进行排序。当我将标题的类型从 String 更改为 LocalizedStringKey 时,出现错误。有没有办法对 localizedStringKey 后面的正确翻译字符串进行排序。
struct Item: Codable,Comparable, Hashable, Identifiable {
static func < (lhs: Item, rhs: Item) -> Bool {
return lhs.title < rhs.title
}
var id: Int
let image: String
let color: String
// title should be LocalizedStringKey
let title: String
}
......
@State private var sortedDown = false
var filteredItems: [Item] {
var sortedItems: [Item]
let filteredItems = modelData.items.filter { item in
(!showFavoritesOnly || item.isFavorite)
}
if sortedDown {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.title > item2.title
})
} else {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.title < item2.title
})
}
return sortedItems
}
var body: some View {
NavigationView {
List {
Toggle(isOn: $showFavoritesOnly, label: {
Text("showFavorites")
})
ForEach(filteredItems) { (item) in
.....
``
LocalizedStringKey
不符合 Comparable
协议,因此不能在其上使用 <
或 >
运算符。
您可以只使用常规 String
,也可以使用自定义比较逻辑。
提问时不要忘记提供 minimal reproducible example。你会得到更好、更有针对性的答案。以下是您可以执行此操作的一种方法。
class LocalizeTitleViewModel: ObservableObject {
@Published var items: [SampleItem] = [SampleItem(title: "orange", isFavorite: true), SampleItem(title: "pink", isFavorite: false), SampleItem(title: "red", isFavorite: true)]
@Published var sortedDown = false
@Published var showFavoritesOnly = false
var filteredItems: [SampleItem] {
var sortedItems: [SampleItem]
let filteredItems = items.filter { item in
(!showFavoritesOnly || item.isFavorite)
}
if sortedDown {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.localizedTitle > item2.localizedTitle
})
} else {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.localizedTitle < item2.localizedTitle
})
}
return sortedItems
}
}
struct LocalizeTitle: View {
@StateObject var modelData: LocalizeTitleViewModel = LocalizeTitleViewModel()
var body: some View {
NavigationView {
List {
Toggle(isOn: $modelData.showFavoritesOnly, label: {
Text("showFavorites")
})
ForEach(modelData.filteredItems) { (item) in
Text(item.localizedTitle)
}
}
}
}
}
struct SampleItem: Identifiable {
var id: UUID = UUID()
let title: String
var isFavorite: Bool
var localizedTitle: String{
get{
//You need a Localizable.strings file in your project that contains all your `title`
return NSLocalizedString(self.title, comment: "Sample Item title")
}
}
}
struct LocalizeTitle_Previews: PreviewProvider {
static var previews: some View {
LocalizeTitle()
}
}
您好,我的问题是我想按 object 的标题 属性 对 object 的数组进行排序。当我将标题的类型从 String 更改为 LocalizedStringKey 时,出现错误。有没有办法对 localizedStringKey 后面的正确翻译字符串进行排序。
struct Item: Codable,Comparable, Hashable, Identifiable {
static func < (lhs: Item, rhs: Item) -> Bool {
return lhs.title < rhs.title
}
var id: Int
let image: String
let color: String
// title should be LocalizedStringKey
let title: String
}
......
@State private var sortedDown = false
var filteredItems: [Item] {
var sortedItems: [Item]
let filteredItems = modelData.items.filter { item in
(!showFavoritesOnly || item.isFavorite)
}
if sortedDown {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.title > item2.title
})
} else {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.title < item2.title
})
}
return sortedItems
}
var body: some View {
NavigationView {
List {
Toggle(isOn: $showFavoritesOnly, label: {
Text("showFavorites")
})
ForEach(filteredItems) { (item) in
.....
``
LocalizedStringKey
不符合 Comparable
协议,因此不能在其上使用 <
或 >
运算符。
您可以只使用常规 String
,也可以使用自定义比较逻辑。
提问时不要忘记提供 minimal reproducible example。你会得到更好、更有针对性的答案。以下是您可以执行此操作的一种方法。
class LocalizeTitleViewModel: ObservableObject {
@Published var items: [SampleItem] = [SampleItem(title: "orange", isFavorite: true), SampleItem(title: "pink", isFavorite: false), SampleItem(title: "red", isFavorite: true)]
@Published var sortedDown = false
@Published var showFavoritesOnly = false
var filteredItems: [SampleItem] {
var sortedItems: [SampleItem]
let filteredItems = items.filter { item in
(!showFavoritesOnly || item.isFavorite)
}
if sortedDown {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.localizedTitle > item2.localizedTitle
})
} else {
sortedItems = filteredItems.sorted(by: { (item1, item2) -> Bool in
return item1.localizedTitle < item2.localizedTitle
})
}
return sortedItems
}
}
struct LocalizeTitle: View {
@StateObject var modelData: LocalizeTitleViewModel = LocalizeTitleViewModel()
var body: some View {
NavigationView {
List {
Toggle(isOn: $modelData.showFavoritesOnly, label: {
Text("showFavorites")
})
ForEach(modelData.filteredItems) { (item) in
Text(item.localizedTitle)
}
}
}
}
}
struct SampleItem: Identifiable {
var id: UUID = UUID()
let title: String
var isFavorite: Bool
var localizedTitle: String{
get{
//You need a Localizable.strings file in your project that contains all your `title`
return NSLocalizedString(self.title, comment: "Sample Item title")
}
}
}
struct LocalizeTitle_Previews: PreviewProvider {
static var previews: some View {
LocalizeTitle()
}
}