1

When migrating to swift 3 why does this code no longer work and asks that it expects a ',' separator?

  import UIKit

    class ProfileButton: UIButton {

        func setProfileImage(address: String?) {
            self.setImage(UIImage(named: "iconProfile"), for: [])
            if let img = address {
                if !img.isEmpty {
                    URLSession.shared().dataTask(URLRequest.with: imgRequest(image: img) as URLRequest, completionHandler: {(data: Data?, response: URLResponse?, error: Error?) in
                        if let imageData = data, image = UIImage(data: imageData) {
                            dispatch_async(dispatch_get_main_queue()) {
                                self.setImage(image.profileImage(), forState: .Normal)
                            }
                        } else if let status = (response as? HTTPURLResponse)?.statusCode {
                            print("image loading status code: \(status)")
                        }
                    }).resume()
                }
            }
        }
    }
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
  • 2
    What line is the error on? – Alexander Jun 29 '16 at 21:20
  • One problem would be the dispatch calls which are only available as methods on DispatchXXX types in Swift 3, so this might be a duplicate of http://stackoverflow.com/questions/37801370/how-do-i-dispatch-sync-dispatch-async-dispatch-after-etc-in-swift-3. – Martin R Jun 29 '16 at 21:20
  • Why are you taking an optional string if you're going to unwrap it in an `if let` with no `else`? – Alexander Jun 29 '16 at 21:22

1 Answers1

1

You have a . where there should be a (:

URLSession.shared().dataTask(URLRequest.with: //...

should be:

URLSession.shared().dataTask(URLRequest(with: //...

You have a mild case of pyramid of doom. How's this?

class ProfileButton: UIButton {

    func setProfileImage(address: String) {
        self.setImage(UIImage(named: "iconProfile"), for: [])

        guard !address.isEmpty else { return }

        let request = URLRequest(with: imgRequest(image: address) as URLRequest){ 
            data, response, error in
            if let imageData = data, image = UIImage(data: imageData) {
                dispatch_async(dispatch_get_main_queue()) {
                    self.setImage(image.profileImage(), forState: .Normal)
                }
            } else if let status = (response as? HTTPURLResponse)?.statusCode {
                print("image loading status code: \(status)")
            }
        }

        URLSession.shared().dataTask(request).resume()
    }
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
  • First time hearing the term pyramid of doom @AMomchilov and I get another error with your code so I'm guess that's what you meant. Im a complete noob to the swift 3 URLRequest struct and I don't know why this error is showing from the variable called request. – Laurence Wingo Jun 29 '16 at 22:03
  • Its saying imgRequest is an unresolved identifier – Laurence Wingo Jun 29 '16 at 22:04
  • I kept the same `imgRequest` you had in your code. I have no clue what it is. The "pyramid of doom" is the triangle shape that forms when you start nesting a lot of blocks inside each other. It's usually indicative of poor design. In this case, I transformed some of your `if` statements, and broke things apart into simpler pieces – Alexander Jun 30 '16 at 00:22