【发布时间】:2015-08-18 11:22:16
【问题描述】:
我正在编写一个 Swift 教程,发现 Swift 有一种奇怪的方式来处理多行语句。
首先,我为标准String 类定义了一些扩展:
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 属性的引用,它会再次起作用:
let s3 = str
.toLowercase()
.replace("hello", withString: "goodbye") // -> goodbye world
在 Swift 语言规范中是否有任何内容阻止属性被拆分到自己的行中?
代码Swift Stub。
【问题讨论】: