【背景】
Swift中的代码:
if let handler = blocks.completionHandler { var resp = HTTPResponse() if let hresponse = task.response as? NSHTTPURLResponse { resp.headers = hresponse.allHeaderFields as? Dictionary<String,String> resp.mimeType = hresponse.MIMEType resp.suggestedFilename = hresponse.suggestedFilename resp.statusCode = hresponse.statusCode resp.URL = hresponse.URL } resp.responseObject = response if let code = resp.statusCode where resp.statusCode > 299 { resp.error = self.createError(code) } handler(resp)
出错:
network/HTTPTask.swift:488:25: Variable ‘resp’ was never mutated; consider changing to ‘let’ constant
如图:
【解决过程】
1.先去多了解了解:
搜:
Variable was never mutated consider changing to let constant
参考:
swift – Variable ‘xxx’ was never mutated, consider changing to ‘let’ – Stack Overflow
如果变量本事就是不可变的
那么尽量从var改为let
这样Swift的编译器可以对let进行优化,从而获得更好的性能
再去按照要求去改为:
let resp = HTTPResponse()
即可。
【总结】
如果变量本事就是不可变的,就应该是const的,那么尽量从var改为let
->这样Swift的编译器可以对let进行优化,从而获得更好的性能。
转载请注明:在路上 » 【已解决】Swift提示:Variable was never mutated consider changing to let constant