Why doesn't redeclaration of 'optional binding' create an error?

你说的曾经没有我的故事 提交于 2019-12-13 07:15:52

问题


I am doing this in Playground, but I am not getting any error. Am I not recreating constant range? Is it happening in 2 different scopes? What's happening in the background that makes this not an error?

if let range = add1.rangeOfString(", ") {
    print(add1.substringToIndex(range.startIndex))
    print (range)
}

if let range = add1.rangeOfString(", ") {
    print(add1.substringToIndex(range.startIndex))
    print (range)
}

回答1:


Variables introduced with Optional binding of if-let is local after the let-clause till the end of true-case code block.

So, yes. Your two range reside in 2 different scopes.

(guard-let is another thing.)



来源:https://stackoverflow.com/questions/39215914/why-doesnt-redeclaration-of-optional-binding-create-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!