gpt4 book ai didi

swift - 无法将类型 'Int' 的值转换为预期参数类型 'UInt'

转载 作者:行者123 更新时间:2023-11-30 12:55:57 28 4
gpt4 key购买 nike

我试图将 insertspaceInExpiryDateTextField() 函数调用到 textField() 函数中并收到上述错误。

//使到期日期文本字段在 2 位数字后采用“/”。

func insertspaceInExpiryDateTextField(string: String,inout andPreserveCursorPosition cursorPosition: UInt) -> String {

var stringWithAddedSpaces: String = ""
for index in 0.stride(to: string.characters.count, by: 1) {
if index != 0 && index % 2 == 0 {
stringWithAddedSpaces += " "

if index < Int(cursorPosition) {
cursorPosition += 1
}
}
let characterToAdd: Character = Array(string.characters) [index]
stringWithAddedSpaces.append(characterToAdd)
}
return stringWithAddedSpaces
}

//将上面的函数调用到这个函数中//使到期日期字段仅采用 4 位数字....还在 2 位数字后给出“/”以划分月份和年份。

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {



if textField == self.txt_expDate {

self.insertspaceInExpiryDateTextField(textField.text!, andPreserveCursorPosition: (&UInt(range.length))) // Here I get the error

txt_expDate.text! = txt_expDate.text!.stringByReplacingOccurrencesOfString(" ", withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)

return ((txt_expDate.text?.characters.count)! >= 4 && range.length == 0) ? false : true
}
}

//除了 UInt 错误之外,其他一切都正常。

//我还检查了 Error 'cannot convert value of type 'int' to expected argument type 'UInt'但没有用..

我有什么特别的地方出错了吗?谢谢谢谢

最佳答案

NSRange 的 length 属性在 Swift 中桥接为 Int 而不是 UInt(我不知道为什么; its NSUInteger in Objective C) 。您可以按如下方式检查 Playground :

print(type(of: NSRange(location: 0, length: 0).length))

关于swift - 无法将类型 'Int' 的值转换为预期参数类型 'UInt',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40387325/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com