When implementing the search function with Swift
Even if you add the following code to the ViewController (SearchViewController) that you added to the Tab Controller, the data will appear without the contentsString
What should I do?
Additional information
I also saw filter as a search function that does not use contentsString, but which one should I use?
import UIKit
classSearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate{
@IBOutlet weak var testTableView: UITableView!
@IBOutlet weak var testSearchBar: UISearchBar!
let dataList: String = [ ]
// Search result array
varsearchResult= [String]()
// A method from the beginning
override func viewDidLoad(){
super.viewDidLoad()
// Set the destination to yourself.
testSearchBar.delegate=self
// Allows you to press Return even if nothing is entered.
testSearchBar.enablesReturnKeyAutomatically=false
// Copy the data to the search result array.
searchResult=dataList
}
// Methods for returning data
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
return dataList.count
}
functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
let searchcell=tableView.dueReusableCell(withIdentifier: "searchcell")!
searchcell.textLabel?.text=dataList [indexPath.row]
// Method for returning the number of data
functableView (tableView: UITableView, numberOfRowsInSection: Int) - > Int {
return searchResult.count
}
// Call method when pressing search button
funcsearchBarSearchButtonClicked (searchBar:UISearchBar) {
testSearchBar.endEditing(true)
// Empty the search result array.
searchResult.removeAll()
if(testSearchBar.text==""){
// Displays all search strings if they are empty.
searchResult=dataList
} else{
// Data including a retrieval character string are added to a retrieval result array.
for data in dataList {
if data.containsString(testSearchBar.text!){
searchResult.append(data)
}
}
}
// Reload the table.
testTableView.reloadData()
}
}
}
First of all, Tony has a few things to know about software development and programming using Swift and Xcode.Before we begin answering questions, let me tell you about the essential knowledge.
1. The programming language Swift is a new language with the first version released in 2014, and is still in the process of development in 2020, with a variety of enhancements and changes.There are many details about the expansion and modification, but sometimes there are large-scale modifications and 180-degree policy changes.
containsString(_string:String)
This method name is
contains(_str:String)
Changes to are common and often result in a sample code written a few years ago that does not work at all with the latest version of Xcode and does not have any "containsString".
Therefore, the properties and methods used for the first time must correspond to the Xcode included domination and reference.When you study English, the first English word you see is the same as looking up an English-Japanese dictionary.
Second, the standard Apple product development language is Swift, but before 2014, it was Objective-C.Objective-C became Apple's (formerly Next) development language in 1985, so it's a fairly old language, but the basic iOS framework (Foundation, UIKit) is described in this Objective-C, and Swift's development of iOS software is based on Objective-C compatibility.
let dataList: String = [ ]
for dataList{
if data.containsString(testSearchBar.text!){
searchResult.append(data)
}
}
The variable data
in is of type String
.However, no matter how much you look for references to the structure String
,
Neither containsString(_string:String)
nor contains(_str:String)
methods are found.This method is actually located in class NSString
.NSString
is originally an Objective-C class, and Swift's structure String
is guaranteed to be compatible with it.
if data.containsString(testSearchBar.text!)
if(data as NSSstring).contains(testSearchBar.text!)
Then you'll be on the Xcode input complement list, where as
is the operator you'll use to make a typecast.
Third, Objective-C and Swift compatibility are not guaranteed in the future.At this point, SwiftUI, a unique Swift framework that does not involve Objective-C at all, is about to be put to practical use.From now on, you will need to assume a code description that completes with Swift only.
I also saw filter as a search function that does not use contentsString, but which one should I use?
If I were to use it, it would be in the protocol StringProtocol
,
func contains(_other:T)->Boolwhere T:StringProtocol
should be adopted.
Have you learned the word "protocol" (or concept)?If it's the first time I've seen it, it's an important concept that forms the basis of Swift's linguistic system (which is actually the same in Objective-C), so I should sit back a little and study it carefully.
However, if you want to follow this question, the protocol can be seen as a component of a mold, such as a class or structure.It's not accurate, but if you look at only one side, you can do that.
I will look at it step by step.First, I would like to check if the string "Ai"
contains the string "Ai"
.Find out if there are any functions or methods that can do so.Since the target is a string, look for a reference to see if it is not in the method of type String
.Look for an English word for "included".However, I can't find the right one.Now, we'll look at each of the protocols listed in Conforms To at the end of the String
reference.Then we found the method func contains<T>(_other:T)->Boolwhere T:StringProtocol
in the protocol StringProtocol
.I think I can use this.Any method that is not in a structure, class, or compliant protocol can be used in that structure.
let aiueo="Aiueo"
if aiueo.contains("Ai"){
print("Contains")
}
This is how you find the desired method, properties, etc.
There's one thing I have to point out, far from the question.
let dataList: String = [ ]
If you declare properties in let
, they are fixed as an array of zero elements, and you cannot change them from zero. You must learn the difference between let
and var
.
© 2024 OneMinuteCode. All rights reserved.