加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift中的多行语句

发布时间:2020-12-14 04:42:18 所属栏目:百科 来源:网络整理
导读:我正在研究 Swift教程,发现 Swift有一种奇怪的方式来处理多行语句. 首先,我定义了标准String类的一些扩展: extension String { func replace(target: String,withString: String) - String { return self.stringByReplacingOccurrencesOfString(target,with
我正在研究 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.

解决方法

这绝对是一个编译器错误.问题已在Xcode 7 beta 3中得到解决.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读