我的任务是创建一个函数,该函数接受字符串和 int 两个参数,该函数将返回一个字符串,该字符串重复字符串参数 int 的次数。这是我想到的,但出现错误。顺便说一句,这是我在工作时在 CodeCardio 上做的,所以我无法在 Xcode 中测试它(我的工作很糟糕并且使用 Windows)
func repeater(aString: String, withNumber: Int) -> String {
let repeatedString = String(count: withNumber, repeatedValue: aString)
return repeatedString
}
String(count: withNumber, repeatedValue: aString)
用于实例化具有重复字符的字符串:Swift init(count:,repeatedValue:) 有效吗?
相反做
func repeater(string: String, withNumber number: Int) -> String {
var repeatedString = String()
for i in 0..<number {
repeatedString += string
}
return repeatedString
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)