首页 新闻 会员 周边

关于swift语法问题

0
[已解决问题] 解决于 2018-05-24 14:04
func count(string: String) -> (vowels: Int, consonants: Int, others: Int) {
    var vowels = 0, consonants = 0, others = 0
    for character in string {
        switch String(character).lowercaseString {
        case "a", "e", "i", "o", "u":
            ++vowels
        case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
          "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
            ++consonants
        default:
            ++others
        }
    }
    return (vowels, consonants, others)
}

请问这段代码为什么不会报错?

明明它把参数里的也声明了一遍。。。

山抹微云君^^的主页 山抹微云君^^ | 菜鸟二级 | 园豆:274
提问于:2018-05-24 10:09
< >
分享
最佳答案
0

这个问题是由于swift默认把函数参数全部默认声明成了常量类型,所以再次用同样的名字声明一个变量是可以的

山抹微云君^^ | 菜鸟二级 |园豆:274 | 2018-05-24 14:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册