'x' 类型的值没有成员“小写”
Value of type 'x' has no member 'lowercased
我正在尝试在我的 table 视图中实现搜索栏。但我收到这个错误
类型 'getUserData' 的值在此代码
上没有成员 'lowercased'
filteredDataBySearch = dataSourceArray.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
这是我的代码:
@IBOutlet weak var FSuserLIst: UITableView!
var dataSourceArray1 = [getUserData]()
var dataSourceArray2 = [getUserData]()
var dataSourceArray3 = [getUserData]()
var dataSourceArray4 = [getUserData]()
let searchBar = UISearchBar()
var showSearchResults = false
var filteredDataBySearch = [getUserData]()
这是我的搜索功能
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
showSearchResults = true
searchBar.endEditing(true)
self.FSuserLIst.reloadData()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if segmentedData.selectedSegmentIndex == 0 {
filteredDataBySearch = dataSourceArray.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
else if segmentedData.selectedSegmentIndex == 1 {
filteredDataBySearch = dataSourceArray1.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
else if segmentedData.selectedSegmentIndex == 2 {
filteredDataBySearch = dataSourceArray2.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
else if segmentedData.selectedSegmentIndex == 3 {
filteredDataBySearch = dataSourceArray3.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
}
"getUserData" 是具有超过 10 个变量的 class,它位于另一个 swift 文件中。
非常感谢任何帮助,谢谢。
您正在尝试将 getUserData class 转换为小写。您想要访问此 class 中的字符串,然后将其转换。我还没有测试过这个,但你正在寻找类似的东西。
filteredDataBySearch = dataSourceArray.filter({ (userData:MyUserDataClass) -> Bool in
let data = userData.myStringInThisClass.lowercased()
return data.contains(searchText.lowercased())
})
这将在 class 中获取一个字符串变量并将其转换为小写。
作为旁注,典型的约定是使用大写字母作为 class 名称,即。 MyClass
我正在尝试在我的 table 视图中实现搜索栏。但我收到这个错误 类型 'getUserData' 的值在此代码
上没有成员 'lowercased'filteredDataBySearch = dataSourceArray.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
这是我的代码:
@IBOutlet weak var FSuserLIst: UITableView!
var dataSourceArray1 = [getUserData]()
var dataSourceArray2 = [getUserData]()
var dataSourceArray3 = [getUserData]()
var dataSourceArray4 = [getUserData]()
let searchBar = UISearchBar()
var showSearchResults = false
var filteredDataBySearch = [getUserData]()
这是我的搜索功能
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
showSearchResults = true
searchBar.endEditing(true)
self.FSuserLIst.reloadData()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if segmentedData.selectedSegmentIndex == 0 {
filteredDataBySearch = dataSourceArray.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
else if segmentedData.selectedSegmentIndex == 1 {
filteredDataBySearch = dataSourceArray1.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
else if segmentedData.selectedSegmentIndex == 2 {
filteredDataBySearch = dataSourceArray2.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
else if segmentedData.selectedSegmentIndex == 3 {
filteredDataBySearch = dataSourceArray3.filter({ (data:String) -> Bool in
return data.lowercased().rangeOfString(searchText.lowercased()) != nil
})
if searchText != "" {
showSearchResults = true
self.FSuserLIst.reloadData()
} else {
showSearchResults = false
self.FSuserLIst.reloadData()
}
}
}
"getUserData" 是具有超过 10 个变量的 class,它位于另一个 swift 文件中。
非常感谢任何帮助,谢谢。
您正在尝试将 getUserData class 转换为小写。您想要访问此 class 中的字符串,然后将其转换。我还没有测试过这个,但你正在寻找类似的东西。
filteredDataBySearch = dataSourceArray.filter({ (userData:MyUserDataClass) -> Bool in
let data = userData.myStringInThisClass.lowercased()
return data.contains(searchText.lowercased())
})
这将在 class 中获取一个字符串变量并将其转换为小写。
作为旁注,典型的约定是使用大写字母作为 class 名称,即。 MyClass