How to fix error Can't invoke 'findObjectInBackgroundWithBlock' with an argument list of type ((AnyObject!, NSError!) - Void)
If you are using swift 1.2 (Xcode 6.3) you need to call the function by:
var data = Query.findObjectsInBackgroundWithBlock({(objects:[AnyObject]?, error:NSError?) -> Void in
})
And if you are using swift 1.1 (Xcode 6.1, 6.2) you need to call the function by:
var data = Query.findObjectsInBackgroundWithBlock({(objects:[AnyObject]?, error:NSError!) -> Void in
})
This is different because of the swift update 1.2 which has changes with using optionals.
Loading/Downloading image from URL on Swift
Xcode 8 or later • Swift 3 or later
Synchronously:
if let filePath = Bundle.main.path(forResource: "imageName", ofType: "jpg"), let image = UIImage(contentsOfFile: filePath) {
imageView.contentMode = .scaleAspectFit
imageView.image = image
}
Asynchronously:
Create a method with a completion handler to get the image data from your url
func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}
Create a method to download the image (start the task)
func downloadImage(from url: URL) {
print("Download Started")
getData(from: url) { data, response, error in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
// always update the UI from the main thread
DispatchQueue.main.async() { [weak self] in
self?.imageView.image = UIImage(data: data)
}
}
}
Usage:
override func viewDidLoad() {
super.viewDidLoad()
print("Begin of code")
let url = URL(string: "https://cdn.arstechnica.net/wp-content/uploads/2018/06/macOS-Mojave-Dynamic-Wallpaper-transition.jpg")!
downloadImage(from: url)
print("End of code. The image will continue downloading in the background and it will be loaded when it ends.")
}
Extension:
extension UIImageView {
func downloaded(from url: URL, contentMode mode: ContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { [weak self] in
self?.image = image
}
}.resume()
}
func downloaded(from link: String, contentMode mode: ContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloaded(from: url, contentMode: mode)
}
}
Usage:
imageView.downloaded(from: "https://cdn.arstechnica.net/wp-content/uploads/2018/06/macOS-Mojave-Dynamic-Wallpaper-transition.jpg")
Related Topics
Error "[Sharesheet] Connection Invalidated" Error iOS13+ But Not on iOS 11.4
Cocoa: Simulating Command+Tab in Cgevent
Avspeechrecognizer: Required Condition Is False: _Recordingtap == Nil Error in Swift3
How to Map Over The Values of Zip2
How to Assign a Generic Function to a Variable
How to Declare Swift Generic for Enums of Particular Type
How to Retrieve The Name of Participants in Msconversation
How to Cast from Cftyperef to Axuielement in Swift
Swift Protocol Extension Implementing Another Protocol with Shared Associated Type
Swift: Binary Operator '==' Cannot Be Applied to Operands of Type "Protocol"
Type 'Bundle' Has No Member "Module"
How to Create a Cocoapod with .Swift
Member Operator '==' Must Have at Least One Argument of Type