代码之家  ›  专栏  ›  技术社区  ›  Tony Merritt

更改子地址后保留json数据。iOS,斯威夫特

  •  -1
  • Tony Merritt  · 技术社区  · 6 年前

    我正在获取json数据并对其进行解析,但是由于某种原因,当我将地址更改为不同的json并调用函数时(我在album search中更改了一个eliment),我在json中获得了相同的数据。我想它已经被缓存了。

    这是我的地址:

    var SEARCH_RESULTS_ADDRESS = "\(BASE_URL)\(choiceSearch!).search&\(choiceSearch!)=\(albumSearch)&api_key=\(API_KEY)&format=json"
    

    这是我的AddressDataService函数。

    func getAlbumData(completion: @escaping (_ finished: Bool) -> ()) {
        guard let url = URL(string: SEARCH_RESULTS_ADDRESS) else { return }
    
        URLSession.shared.dataTask(with: url) { (data, response, err) in
    
            guard let data = data else { return }
            do {
    
                let decoder = JSONDecoder()
                let albumDataFull = try decoder.decode(Root.self, from: data)
                albumInfo = []
                for business in albumDataFull.results.albummatches.album {
                    let artist = business.artist!
                    let name = business.name!
                    let imgUrl = business.image
    
                    let albumInd = ["name":name, "artist":artist, "url":url, "imgUrl":imgUrl] as [String : Any]
    
                    albumInfo.append(albumInd)
                    print("Tony: \(albumInfo.count)")
    
                }
    
                completion(true)
            }catch let jsonErr {
                print("Error seroalizing json", jsonErr)
            }
            }.resume()
    }
    

    这是我的转载数据和地址变更。

    func getData(completion: @escaping (_ finished: Bool) -> ()) {
        AlbumDataService().getAlbumData(completion: { (complete) in
            completion(true)
    
        })
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        albumSearch = searchBar.text
        albumInfo = []
        getData(completion: { (complete) in
            DispatchQueue.main.async {
                SEARCH_RESULTS_ADDRESS = "\(BASE_URL)\(choiceSearch!).search&\(choiceSearch!)=\(albumSearch!)&api_key=\(API_KEY)&format=json"
                print(SEARCH_RESULTS_ADDRESS)
                self.tableView.reloadData()
                print("Tony Finishedediting \(albumSearch!)")
            }
        })
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   OOPer    6 年前

    你需要设置 SEARCH_RESULTS_ADDRESS 它被使用了。

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        albumSearch = searchBar.text
        albumInfo = []
        //###
        SEARCH_RESULTS_ADDRESS = "\(BASE_URL)\(choiceSearch!).search&\(choiceSearch!)=\(albumSearch!)&api_key=\(API_KEY)&format=json"
        print(SEARCH_RESULTS_ADDRESS)
        getData(completion: { (complete) in
            DispatchQueue.main.async {
                self.tableView.reloadData()
                print("Tony Finishedediting \(albumSearch!)")
            }
        })
    }
    


    (补充)

    假设您可以修改 AddressDataService :

    func getAlbumData(_ urlString: String, completion: @escaping (_ finished: Bool) -> ()) {
        print(urlString)
        guard let url = URL(string: urlString) else {
            print("ursString is invalid")
            return
        }
    
        //...
    }
    

    你可以把它当作:

    func getData(_ urlString: String, completion: @escaping (_ finished: Bool) -> ()) {
        AlbumDataService().getAlbumData(urlString)  { complete in
            completion(true)
        }
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        albumSearch = searchBar.text
        albumInfo = []
        let searchResultsAddress = "\(BASE_URL)\(choiceSearch!).search&\(choiceSearch!)=\(albumSearch!)&api_key=\(API_KEY)&format=json"
        getData(searchResultsAddress) { complete in
            DispatchQueue.main.async {
                self.tableView.reloadData()
                print("Tony Finishedediting \(albumSearch!)")
            }
        }
    }