Swift 尝试实现搜索栏时出错
Swift error while trying to implement a search bar
我正在尝试在我的代码中实现一个搜索栏,但出于某种原因我收到了错误,Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
,这仅在实现 if 语句但与 print 语句配合使用时才会发生。 If 语句有问题还是我用错了?我希望它的工作方式是显示所有核心数据条目,除非在搜索栏中输入了某些内容(我假设在 If 语句中使用 or 语句来检查 search == "")以及是否输入了某些内容搜索栏仅显示名称属性包含搜索栏显示内容的条目。我正在使用 Xcode 12.5。任何帮助将不胜感激。
import SwiftUI
struct customers: View {
@FetchRequest(entity: Customer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Customer.name, ascending: true)]) var customered: FetchedResults<Customer>
@Environment(\.managedObjectContext) var moc
@State var search = String()
var body: some View {
TextField("Search", text: $search)
VStack{
HStack{
Text("Name")
}
Group{
List(customered) {that in
if(that.name?.lowercased().contains(search.lowercased())){
Text(that.name ?? "unknown")
.onTapGesture {
if(that.shown){
that.shown = false
}else{
for shownVar in customered{
print(shownVar.name?.lowercased().contains(search.lowercased()))
}
}
}
}
}
}
}
}
P.S。回想一下这也需要很长时间来编译和加载所以有没有更快更好的方法因为有一次它不会构建因为它花费了太长时间并且说要分解代码(这是一个更短的版本实际代码)。
我认为问题是“that.name”可以为零,因此编译器无法理解该做什么。尝试这样的事情来实现你想要的:
if search.isEmpty {
Text(that.name ?? "unknown")
.onTapGesture {
// ....
}
} else {
if ( (that.name ?? "").lowercased().contains(search.lowercased())) {
Text(that.name ?? "unknown")
.onTapGesture {
// ....
}
}
}
编辑:
为了简洁起见,你使用这样的东西:
struct CustomersView: View {
@FetchRequest(entity: Customer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Customer.name, ascending: true)]) var customered: FetchedResults<Customer>
@Environment(\.managedObjectContext) var moc
@State var search = String()
var body: some View {
TextField("Search", text: $search)
VStack{
HStack{
Text("Name")
}
Group{
List($customered, id: \.id) { $that in
if search.isEmpty {
TapableTextView(cust: $that)
} else {
if ( (that.name ?? "").lowercased().contains(search.lowercased())) {
TapableTextView(cust: $that)
}
}
}
}
}
}
}
struct TapableTextView: View {
@Binding var cust: customer
var body: some View {
Text(cust.name ?? "unknown")
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.onTapGesture {
if(cust.shown){
cust.shown = false
}
}
}
}
编辑 2:我刚刚注意到您使用的是 Xcode 12.5,在这种情况下,请尝试这样的操作:
struct CustomersView: View {
@FetchRequest(entity: Customer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Customer.name, ascending: true)]) var customered: FetchedResults<Customer>
@Environment(\.managedObjectContext) var moc
@State var search = String()
private func tapableText(_ ndx: Int) -> some View {
Text(customered[ndx].name ?? "unknown")
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.onTapGesture {
if(customered[ndx].shown){
customered[ndx].shown = false
}
print( "tapped on " + (customered[ndx].name ?? "unknown") )
}
}
var body: some View {
TextField("Search", text: $search)
VStack{
HStack{
Text("Name")
}
Group{
List(customered.indices, id: \.self) { ndx in
if search.isEmpty {
tapableText(ndx)
} else {
if ( (customered[ndx].name ?? "").lowercased().contains(search.lowercased())) {
tapableText(ndx)
}
}
}
}
}
}
}
我正在尝试在我的代码中实现一个搜索栏,但出于某种原因我收到了错误,Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
,这仅在实现 if 语句但与 print 语句配合使用时才会发生。 If 语句有问题还是我用错了?我希望它的工作方式是显示所有核心数据条目,除非在搜索栏中输入了某些内容(我假设在 If 语句中使用 or 语句来检查 search == "")以及是否输入了某些内容搜索栏仅显示名称属性包含搜索栏显示内容的条目。我正在使用 Xcode 12.5。任何帮助将不胜感激。
import SwiftUI
struct customers: View {
@FetchRequest(entity: Customer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Customer.name, ascending: true)]) var customered: FetchedResults<Customer>
@Environment(\.managedObjectContext) var moc
@State var search = String()
var body: some View {
TextField("Search", text: $search)
VStack{
HStack{
Text("Name")
}
Group{
List(customered) {that in
if(that.name?.lowercased().contains(search.lowercased())){
Text(that.name ?? "unknown")
.onTapGesture {
if(that.shown){
that.shown = false
}else{
for shownVar in customered{
print(shownVar.name?.lowercased().contains(search.lowercased()))
}
}
}
}
}
}
}
}
P.S。回想一下这也需要很长时间来编译和加载所以有没有更快更好的方法因为有一次它不会构建因为它花费了太长时间并且说要分解代码(这是一个更短的版本实际代码)。
我认为问题是“that.name”可以为零,因此编译器无法理解该做什么。尝试这样的事情来实现你想要的:
if search.isEmpty {
Text(that.name ?? "unknown")
.onTapGesture {
// ....
}
} else {
if ( (that.name ?? "").lowercased().contains(search.lowercased())) {
Text(that.name ?? "unknown")
.onTapGesture {
// ....
}
}
}
编辑:
为了简洁起见,你使用这样的东西:
struct CustomersView: View {
@FetchRequest(entity: Customer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Customer.name, ascending: true)]) var customered: FetchedResults<Customer>
@Environment(\.managedObjectContext) var moc
@State var search = String()
var body: some View {
TextField("Search", text: $search)
VStack{
HStack{
Text("Name")
}
Group{
List($customered, id: \.id) { $that in
if search.isEmpty {
TapableTextView(cust: $that)
} else {
if ( (that.name ?? "").lowercased().contains(search.lowercased())) {
TapableTextView(cust: $that)
}
}
}
}
}
}
}
struct TapableTextView: View {
@Binding var cust: customer
var body: some View {
Text(cust.name ?? "unknown")
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.onTapGesture {
if(cust.shown){
cust.shown = false
}
}
}
}
编辑 2:我刚刚注意到您使用的是 Xcode 12.5,在这种情况下,请尝试这样的操作:
struct CustomersView: View {
@FetchRequest(entity: Customer.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Customer.name, ascending: true)]) var customered: FetchedResults<Customer>
@Environment(\.managedObjectContext) var moc
@State var search = String()
private func tapableText(_ ndx: Int) -> some View {
Text(customered[ndx].name ?? "unknown")
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.onTapGesture {
if(customered[ndx].shown){
customered[ndx].shown = false
}
print( "tapped on " + (customered[ndx].name ?? "unknown") )
}
}
var body: some View {
TextField("Search", text: $search)
VStack{
HStack{
Text("Name")
}
Group{
List(customered.indices, id: \.self) { ndx in
if search.isEmpty {
tapableText(ndx)
} else {
if ( (customered[ndx].name ?? "").lowercased().contains(search.lowercased())) {
tapableText(ndx)
}
}
}
}
}
}
}