I've dug around * and found the solution which I converted to Swift, it doesn't seem to work and the selector is still being performed.
我已经挖掘了*并找到了我转换为Swift的解决方案,它似乎不起作用,选择器仍在执行。
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
self.filter.searchTerm = self.searchBar.text
NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: "getHints", object: nil)
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "getHints", userInfo: nil, repeats: false)
}
Is there a better way to do this in swift? Thanks!
在swift中有更好的方法吗?谢谢!
1 个解决方案
#1
28
UPDATE 2016/09/01:
更新2016/09/01:
We can use NSTimers
or (since swift 2.0) NSObject's performSelector
and friends.
我们可以使用NSTimers或(自swift 2.0)NSObject的performSelector和朋友。
Aproach 1 : performSelector
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
NSObject.cancelPreviousPerformRequests(
withTarget: self,
selector: #selector(ViewController.getHintsFromTextField),
object: textField)
self.perform(
#selector(ViewController.getHintsFromTextField),
with: textField,
afterDelay: 0.5)
return true
}
func getHintsFromTextField(textField: UITextField) {
print("Hints for textField: \(textField)")
}
Approach 2: NSTimer
var timer: NSTimer? = nil
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
timer?.invalidate()
timer = Timer.scheduledTimer(
timeInterval: 0.5,
target: self,
selector: #selector(ViewController.getHints),
userInfo: ["textField": textField],
repeats: false)
return true
}
func getHints(timer: Timer) {
var userInfo = timer.userInfo as! [String: UITextField]
print("Hints for textField: \(userInfo["textField"])")
}
Note I am passing the textField
to delayed functions. It is not always required but it could make your life easier when textField
is not easy to access or when dealing with various text fields.
注意我将textField传递给延迟函数。它并不总是必需的,但是当textField不易访问或处理各种文本字段时,它可以使您的生活更轻松。
How NSTimer
approach is different from performSelector
?
When you call performSelector
the target is retained (in swift the target is always self
) but when you use NSTimer
the target is NOT retained. This means, if you use NSTimer
s, you have to make sure target (in this case self
) is alive by the time the timer fires. Otherwise the a crash will occur.
当你调用performSelector时,目标被保留(在swift中,目标总是自己)但是当你使用NSTimer时,目标不会被保留。这意味着,如果您使用NSTimers,则必须确保在计时器触发时目标(在本例中为self)处于活动状态。否则会发生崩溃。
(BTW: performSelector
uses NSTimer
internally ???? )
(顺便说一句:performSelector内部使用NSTimer????)
If you are interested in GCD timers this gist a good place start: maicki/TimerWithGCD.md
如果你对GCD计时器感兴趣,那么这个要点就是一个好的开始:maicki / TimerWithGCD.md
#1
28
UPDATE 2016/09/01:
更新2016/09/01:
We can use NSTimers
or (since swift 2.0) NSObject's performSelector
and friends.
我们可以使用NSTimers或(自swift 2.0)NSObject的performSelector和朋友。
Aproach 1 : performSelector
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
NSObject.cancelPreviousPerformRequests(
withTarget: self,
selector: #selector(ViewController.getHintsFromTextField),
object: textField)
self.perform(
#selector(ViewController.getHintsFromTextField),
with: textField,
afterDelay: 0.5)
return true
}
func getHintsFromTextField(textField: UITextField) {
print("Hints for textField: \(textField)")
}
Approach 2: NSTimer
var timer: NSTimer? = nil
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
timer?.invalidate()
timer = Timer.scheduledTimer(
timeInterval: 0.5,
target: self,
selector: #selector(ViewController.getHints),
userInfo: ["textField": textField],
repeats: false)
return true
}
func getHints(timer: Timer) {
var userInfo = timer.userInfo as! [String: UITextField]
print("Hints for textField: \(userInfo["textField"])")
}
Note I am passing the textField
to delayed functions. It is not always required but it could make your life easier when textField
is not easy to access or when dealing with various text fields.
注意我将textField传递给延迟函数。它并不总是必需的,但是当textField不易访问或处理各种文本字段时,它可以使您的生活更轻松。
How NSTimer
approach is different from performSelector
?
When you call performSelector
the target is retained (in swift the target is always self
) but when you use NSTimer
the target is NOT retained. This means, if you use NSTimer
s, you have to make sure target (in this case self
) is alive by the time the timer fires. Otherwise the a crash will occur.
当你调用performSelector时,目标被保留(在swift中,目标总是自己)但是当你使用NSTimer时,目标不会被保留。这意味着,如果您使用NSTimers,则必须确保在计时器触发时目标(在本例中为self)处于活动状态。否则会发生崩溃。
(BTW: performSelector
uses NSTimer
internally ???? )
(顺便说一句:performSelector内部使用NSTimer????)
If you are interested in GCD timers this gist a good place start: maicki/TimerWithGCD.md
如果你对GCD计时器感兴趣,那么这个要点就是一个好的开始:maicki / TimerWithGCD.md