Swift 无法转换类型";Int.Type";输入“;Int";-函数参数-setw C++;
我一直在寻找swift中的一些等价物,但没有找到。因此,我编写了一个快速函数,或多或少实现了相同的目标,但当使用Swift 无法转换类型";Int.Type";输入“;Int";-函数参数-setw C++;,swift,function,Swift,Function,我一直在寻找swift中的一些等价物,但没有找到。因此,我编写了一个快速函数,或多或少实现了相同的目标,但当使用toprint作为参数时,我得到一个生成错误,表示Int.Type无法转换为Int。有没有办法解决这个问题 // FUNCTION WITH ERROR func space (toprint: Int) { var spaces = [" ", " ", " ", " ", " ", " ", " "] var digit
toprint
作为参数时,我得到一个生成错误,表示Int.Type
无法转换为Int
。有没有办法解决这个问题
// FUNCTION WITH ERROR
func space (toprint: Int) {
var spaces = [" ", " ", " ", " ", " ", " ", " "]
var digits = [Int] ()
var toprinttemp = toprint
while toprinttemp >= 1 {
digits.append(toprint%10)
toprinttemp = toprinttemp/10
}
let count = (digits.count - 1)
print("(\(spaces[count])", terminator:"")
}
// Print top row
var counter = 0
while (counter) <= (rows - 1) {
if (tlcorner - counter) <= userinput {
var toprint:Int = (tlcorner - counter)
print("\(toprint)", terminator:"")
// Calling Function
space(toprint:Int)
}
counter += 1
}
//函数出错
func空间(toprint:Int){
变量空间=[“”、“”、“”、“”、“”、“”、“”、“”、“”、“”]
变量位数=[Int]()
var toprinttemp=toprint
而toprinttemp>=1{
数字。追加(toprint%10)
toprinttemp=toprinttemp/10
}
让计数=(digits.count-1)
打印(“(\(空格[count])”,终止符:“”)
}
//打印顶行
变量计数器=0
while(counter)您需要将Int
传递给函数,而不是类型Int
。
可以这样称呼:
space(toprint: 5)
请注意,toprint
这里是space
函数的参数名称,它不是要传入的Int
。在您的情况下,您需要:
space(toprint: toprint)
这里,第一个toprint是参数标签,第二个是Int
如果您的目标只是获取一个具有可变空格数的字符串,则可以执行以下操作:
String(repeating: " ", count: 10)
读取其他的方法。< /P>删除C++标签,因为问题不是关于C++,而是只是稍微相关的。@ BaMuMutGun不发布一个关于为什么编辑的单独注释。指定注释是编辑的一部分。为什么代码中有所有的额外括号?谢谢!我不能精确地指定TopRink的值,但是,它是C。每个循环都会改变。还有其他方法吗?我在注意到变量名也是
toprint
后更新了答案。我认为您将参数标签与Int
变量混淆了。