objective-c - Swift 里 guard 的里面不能用变量吗 , 那我想修改怎么办。
高洛峰
高洛峰 2017-05-02 09:24:41
0
2
481

//我想在guard 改变num2的值.可是报错了
func demoGuard() {

let oNum: Int? = nil guard var num2 = oNum else{ num2 = 10 return }

}

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (2)
为情所困

The scope of num2 is only after guard, but it returns when it goes to else. What is the meaning of changing the value of num2 at this time? I assume you want to change the value of oNum? In this case, you can change let oNum to var oNum in guard, but the same thing is meaningless. It may also make sense if the value you want to change is the value of an inout type parameter passed in during an external call.

    过去多啦不再A梦

    This requirement is very strange, it is better to use the ?? operator

    num2 = oNum ?? 10
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!