最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Swift提示:Variable was never mutated consider changing to let constant

Swift crifan 4588浏览 0评论

【背景】

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

如图:

Variable 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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.214 seconds, using 22.09MB memory