gpt4 book ai didi

json - 几秒钟后快速刷新 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 15:31:25 25 4
gpt4 key购买 nike

我希望它每 4 秒更新一次来自 url 的新数据,但我不知道该怎么做。这是我到目前为止所拥有的,它工作正常但没有复习! Refresher 需要像 YouTube 订户计数器一样工作,每 4 秒左右更新一次。我看过一个计时器,但我无法让它工作,因为(我认为)它是一个 searchBarSearchButtonClicked 函数并且 urlRequestid 必须有一个输入!请帮忙!谢谢!

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

let urlRequestid = URLRequest(url: URL(string: "https://www.mylink.com/\(searchBar.text!.replacingOccurrences(of: " ", with: "%20"))/?__a=1")!)

if (interstitial.isReady){
interstitial.present(fromRootViewController: self)
interstitial = createAndLoadInterstitial()
}

let task = URLSession.shared.dataTask(with: urlRequestid) { (data, response, error) in
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]

if let user = json["user"] as? [String : AnyObject] {
let profile_pic_url_hd = user["profile_pic_url_hd"] as! String
let urlstr = "\(profile_pic_url_hd)"
if var comps = URLComponents(string: urlstr) {
var path = comps.path
var pathComps = path.components(separatedBy: "/")
pathComps.remove(at: 2) // this removes the s320x320
path = pathComps.joined(separator: "/")
comps.path = path
if let newStr = comps.string {
print(newStr)
self.imgURL = "\(newStr)"
}
}

if let bio = user["biography"] as? String {
self.bioS = bio
}

if let naam = user["username"] as? String {
self.naamS = naam
}

if let followed_by = user["followed_by"] as? [String : AnyObject] {
self.VolgS = followed_by["count"] as! Int
}

if let follows = user["follows"] as? [String : AnyObject] {
self.volgD = follows["count"] as! Int
}
if let media = user["media"] as? [String : AnyObject] {
self.postS = media["count"] as! Int
}

}

if let _ = json["error"] {
self.exists = false
}

DispatchQueue.main.async {
if self.exists{
self.imgView.downloadImage(from: self.imgURL!)
self.naam.text = "@\(self.naamS ?? "")"
if self.bioS == nil {
self.bio.text = "This Person has no biography!"
} else {
self.bio.text = "\(self.bioS ?? "")"
}
self.volgers.text = "\(self.VolgS!)"
self.volgend.text = "\(self.volgD!)"
self.post.text = "\(self.postS!)"
} else {
self.exists = true
}
}
} catch let jsonError {
print(jsonError.localizedDescription)
}
}
}
task.resume()
}
}

最佳答案

一个快速但公认笨拙的修复方法是将来自 searchBarSearchButtonClicked 参数的最新 UISearchBar 实例存储在本地实例变量中:

var currentSearch: UISearchBar = UISearchBar()
var timer: Timer?

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

currentSearch = searchBar
// Add the rest of the method code below...
...

}

// Call this method to begin repetition
func repeatSearch() {

self.timer = Timer.scheduledTimer(withTimeInterval: 4.0, repeats: true,
block: { (timer) in
self.searchBarSearchButtonClicked(self.currentSearch)
})
}

关于json - 几秒钟后快速刷新 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44701410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com