为什么此代码会出现错误预期','分隔器?

时间:2016-06-29 21:17:17

标签: ios xcode swift swift3

迁移到swift 3时,为什么此代码不再起作用,并要求它预期''分隔器?

  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()
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您有.,其中应该有(

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

应该是:

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

你有一个温和的厄运金字塔案例。怎么样?

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()
    }
}