Swift中的多行语句 [英] Multiline statement in Swift
本文介绍了Swift中的多行语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究Swift教程,发现Swift有一种奇怪的方式来处理多行语句.
I was working on a Swift tutorial and found that Swift has a strange way to handle multi-line statement.
首先,我定义了对标准String
类的一些扩展:
First, I defined some extension to the standard String
class:
extension String {
func replace(target: String, withString: String) -> String {
return self.stringByReplacingOccurrencesOfString(target, withString: withString)
}
func toLowercase() -> String {
return self.lowercaseString
}
}
这可以按预期工作:
let str = "HELLO WORLD"
let s1 = str.lowercaseString.replace("hello", withString: "goodbye") // -> goodbye world
这不起作用:
let s2 = str
.lowercaseString
.replace("hello", withString: "goodbye")
// Error: could not find member 'lowercaseString'
如果我用函数调用替换对lowercaseString
属性的引用,它将再次起作用:
If I replace the reference to the lowercaseString
property with a function call, it works again:
let s3 = str
.toLowercase()
.replace("hello", withString: "goodbye") // -> goodbye world
Swift语言规范中是否有任何东西可以防止某个属性折断在自己的行上?
Is there anything in the Swift language specifications that prevent a property to be broken onto its own line?
位于快速存根的代码.
推荐答案
这绝对是编译器错误. Xcode 7 beta 3中的问题已解决.
This is definitely a compiler bug. Issue has been resolved in Xcode 7 beta 3.
这篇关于Swift中的多行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文