大佬教程收集整理的这篇文章主要介绍了swift是否允许没有条件/循环的代码块来减少局部变量范围?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func MyFunc() { // if statements get block level scope if self@L_254_2@meCondition { var thisVarShouldntexistElsewhere = true self.doSomethingElse(thisVarShouldntexistElsewherE) } // many languages allow blocks without conditions/loops/etc { var thisVarShouldntexistElsewhere = false self.doSomething(thisVarShouldntexistElsewherE) } }
当我在Swift中执行此操作时,它认为我正在创建一个闭包并且不执行代码.我可以创建它作为一个闭包并立即执行,但这似乎会带来执行开销(不值得为代码清洁).
func MyFunc() { // if statements get block level scope if self@L_254_2@meCondition { var thisVarShouldntexistElsewhere = true self.doSomethingElse(thisVarShouldntexistElsewherE) } // converted to closure ({ var thisVarShouldntexistElsewhere = false self.doSomething(thisVarShouldntexistElsewherE) })() }
在Swift中是否支持这样的东西?
func foo() { let x = 5 do { let x = 10 print(X) } } foo() // prints "10"
按照The Swift Programming Language:
以上是大佬教程为你收集整理的swift是否允许没有条件/循环的代码块来减少局部变量范围?全部内容,希望文章能够帮你解决swift是否允许没有条件/循环的代码块来减少局部变量范围?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。