我只是想检测是否已使用我的库成功将数据解析到主ViewController中。如何从库文件功能中检索数据?这里的想法是检索所有数据,使用数据中的URL通过我的第二个函数进行解析,该函数接收字符串并将图像存储在缓存中,然后将其加载回视图控制器中
1
______________________________________________________(不同文件)
class Library {
func parseData() -> [Pintrest] {
let jsonUrlString = "MyLink"
guard let url = URL(string: jsonUrlString) else { return pintrest }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let pintrests = try? JSONDecoder().decode([Pintrest].self, from: data){
pintrest = pintrests
DispatchQueue.main.async {
print(pintrest) // THIS PRINTS THE DATA
}
}
}
}
task.resume()
return pintrest
}
func parseImages(ImagesUrlArrayPath: String){
if let imageURL = URL(string: (ImagesUrlArrayPath)){
DispatchQueue.global().async{
let data = try? Data(contentsOf: imageURL)
if let data = data{
let imageToCache = UIImage(data: data)
// let image = imageToCache
DispatchQueue.main.async {
imageCache.setObject(imageToCache!, forKey: ImagesUrlArrayPath as AnyObject)
print("success")
//cell?.videoImageView.image = image //?.resizeImage(targetSize: size)
}
}
}
}
}
}