最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

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

Swift crifan 1571浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
58 queries in 0.087 seconds, using 18.82MB memory