迁移到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()
}
}
}
}
答案 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()
}
}