我正在研究Swift教程,发现Swift有一种奇怪的方式来处理多行语句.
首先,我定义了标准String
类的一些扩展:
extension String {
func replace(target: String, withString: String) -> String {
return self.stringByReplacingOccurrencesOfString(target, withString: withString)
}
func toLowercase() -> String {
return self.lowercaseString
}
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作:
let str = "HELLO WORLD"
let s1 = str.lowercaseString.replace("hello", withString: "goodbye") // -> goodbye world
Run Code Online (Sandbox Code Playgroud)
这不起作用:
let s2 = str
.lowercaseString
.replace("hello", withString: "goodbye")
// Error: could not find member 'lowercaseString'
Run Code Online (Sandbox Code Playgroud)
如果我用lowercaseString
函数调用替换属性的引用,它再次起作用:
let s3 = str
.toLowercase()
.replace("hello", withString: "goodbye") // -> goodbye world
Run Code Online (Sandbox Code Playgroud)
Swift语言规范中是否有任何内容可以防止将属性划分为自己的行?
Swift Stub的代码.